Skip to content

Instantly share code, notes, and snippets.

@hui
Forked from henriquemenezes/postgresql-set-id-seq.sql
Created October 4, 2018 01:59
Show Gist options
  • Select an option

  • Save hui/2ad2fcf0af857d6dd4a7199a304ec3fb to your computer and use it in GitHub Desktop.

Select an option

Save hui/2ad2fcf0af857d6dd4a7199a304ec3fb to your computer and use it in GitHub Desktop.
PostgreSQL set Next ID Sequence Value to MAX(id) from Table
-- Get Max ID from table
SELECT MAX(id) FROM table;
-- Get Next ID from table
SELECT nextval('table_id_seq');
-- Set Next ID Value to MAX ID
SELECT setval('table_id_seq', (SELECT MAX(id) FROM table));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment