How to create sequence if not exists

后端 未结 6 1180
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 08:17

I tried to use code from Check if sequence exists in Postgres (plpgsql).

To create sequence if it does not exists. Running this code two times causes an exception:

6条回答
  •  一生所求
    2020-12-09 08:47

    Postgres doesn't have CREATE SEQUENCE IF NOT EXISTS and if the table has default value using the sequence if you just drop the sequence, you might get error:

    ERROR: cannot drop sequence (sequence_name) because other objects depend on it SQL state: 2BP01

    For me, this one can help:

    ALTER TABLE  ALTER COLUMN id DROP DEFAULT;
    DROP SEQUENCE IF EXISTS ;
    CREATE sequence ;
    

提交回复
热议问题