Resetting PostgreSQL Sequences

I recently had a problem using serial type primary keys in PostgreSQL, where my seed scripts inserting rows would not update the nextval value of the corresponding sequence. So, I thought I'd share a nice little snippet that will reset such a sequence to the max value available. Can be put at the end of your seed scripts for example.

SELECT pg_catalog.setval('table_table_id_seq', (  
  SELECT MAX(table_id) FROM table
), true);

Frederik Nakstad

Read more posts by this author.

Tokyo, Japan