Skip to content

Instantly share code, notes, and snippets.

@joduplessis
Created August 13, 2019 09:18
Show Gist options
  • Save joduplessis/8f9819b5e34a39b450d589e796213894 to your computer and use it in GitHub Desktop.
Save joduplessis/8f9819b5e34a39b450d589e796213894 to your computer and use it in GitHub Desktop.
PostgreSQL needs to have its Primary Key reset after a table is duplicated - otherwise it runs into the PK ID taken issue. Here's how to reset it to the next highest PK..
SELECT setval('TABLENAME_id_seq', (SELECT MAX(id) FROM "TABLENAME"));
-- Literally replace TABLENAME with your table name:
-- SELECT setval('foo_id_seq', (SELECT MAX(id) FROM "foo"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment