In Postgresql, how can I do a condition to create a table only if it does not already exist?
Code example appreciated.
I'm not sure when it was added, but for the sake of completeness I'd like to point out that in version 9.1 (maybe before) IF NOT EXISTS
can be used. IF NOT EXISTS
will only create the table if it doesn't exist already.
Example:
CREATE TABLE IF NOT EXISTS users.vip
(
id integer
)
This will create a table named vip
in the schema users
if the table doesn't exist.
Source