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

前端 未结 13 2186
星月不相逢
星月不相逢 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 11:10

    Just for simplifying and clarifying the proper usage of ALTER SEQUENCE and SELECT setval for resetting the sequence:

    ALTER SEQUENCE sequence_name RESTART WITH 1;
    

    is equivalent to

    SELECT setval('sequence_name', 1, FALSE);
    

    Either of the statements may be used to reset the sequence and you can get the next value by nextval('sequence_name') as stated here also:

    nextval('sequence_name')
    

提交回复
热议问题