Created
October 23, 2013 12:30
-
-
Save zflat/7117767 to your computer and use it in GitHub Desktop.
Reset-postgres-primary-key-sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Useful when inserting rows into postres (like when seeding data from a .sql dump) messes up the database. | |
http://stackoverflow.com/questions/244243/how-to-reset-postgres-primary-key-sequence-when-it-falls-out-of-sync | |
// 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