Search code examples
postgresqlsyntaxpostgresql-9.1postgresql-9.3create-table

how to to first check whether the table exist or not before creating it in postgresql?


How to check whether a table already exists or not before executing a creation script?

for example in MySQL database, we can do

IF NOT EXISTS tableName ..then create table sql script to follow

how about in PostgreSQL? Is there a way to check first if the table exists before creating it? How do we do it?


Solution

  • PostgreSQL uses the following syntax:

    CREATE TABLE IF NOT EXISTS mytable (
        -- Column definitions...
    )