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

sennin picture sennin · Jan 13, 2011 · Viewed 214.2k times · Source

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?

Answer

Michael Krelin - hacker picture Michael Krelin - hacker · Jan 13, 2011

If you don't want to retain the ordering of ids, then you can

ALTER SEQUENCE seq RESTART WITH 1;
UPDATE t SET idcolumn=nextval('seq');

I doubt there's an easy way to do that in the order of your choice without recreating the whole table.