How to reset a sequence with PostgreSQL

Sun Nov 02 01:49:37 -0700 2008

I would think that

1
2
ALTER SEQUENCE sequence_name
      RESTART WITH (SELECT max(id) FROM table_name);

would work, but it doesn’t. Use:


SELECT SETVAL('sequence_name', (SELECT MAX(id) FROM table_name) + 1);

instead and you will be a lot more happier.

blogLater

Mikel

Leave a Reply