Skip to content

Instantly share code, notes, and snippets.

@derekbarber
Last active August 29, 2015 14:11
Show Gist options
  • Save derekbarber/f69a4e6117414b22c9e8 to your computer and use it in GitHub Desktop.
Save derekbarber/f69a4e6117414b22c9e8 to your computer and use it in GitHub Desktop.
How to reset postgres' primary key sequence when it falls out of sync?
heroku pg:psql
-- Login to psql and run the following
-- What is the result?
SELECT MAX(id) FROM your_table;
-- Then run...
-- This should be higher than the last result.
SELECT nextval('your_table_id_seq');
-- If it's not higher... run this set the sequence last to your highest pid it.
-- (wise to run a quick pg_dump first...)
SELECT setval('your_table_id_seq', (SELECT MAX(id) FROM your_table));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment