How to reset sequence in postgres and fill id column with new data?

前端 未结 13 2184
星月不相逢
星月不相逢 2020-11-27 10:47

I have a table with over million rows. I need to reset sequence and reassign id column with new values (1, 2, 3, 4... etc...). Is any easy way to do that?

13条回答
  •  醉话见心
    2020-11-27 11:06

    With PostgreSQL 8.4 or newer there is no need to specify the WITH 1 anymore. The start value that was recorded by CREATE SEQUENCE or last set by ALTER SEQUENCE START WITH will be used (most probably this will be 1).

    Reset the sequence:

    ALTER SEQUENCE seq RESTART;
    

    Then update the table's ID column:

    UPDATE foo SET id = DEFAULT;
    

    Source: PostgreSQL Docs

提交回复
热议问题