Postgresql: how to create table only if it does not already exist?

后端 未结 10 1102
不思量自难忘°
不思量自难忘° 2021-01-31 08:04

In Postgresql, how can I do a condition to create a table only if it does not already exist?

Code example appreciated.

10条回答
  •  萌比男神i
    2021-01-31 08:32

    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

提交回复
热议问题