How to create sequence if not exists

后端 未结 6 1188
隐瞒了意图╮
隐瞒了意图╮ 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:41

    I have a function to clean all tables in my database application at any time. It is build dynamically, but the essence is that it deletes all data from each table and resets the sequence. This is the code to reset the sequence of one of the tables:

    perform relname from pg_statio_all_sequences where relname = 'privileges_id_seq';
    if found then
      select setval ('privileges_id_seq',1, false) into i_result;
    end if;
    

    Hope this helps,

    Loek

    I am using postgres 8.4, I see that you use 9.2. Could make a difference where the information is stored.

提交回复
热议问题