Skip to content

Instantly share code, notes, and snippets.

@scaryguy
Last active March 21, 2022 15:24
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save scaryguy/6239705 to your computer and use it in GitHub Desktop.
Save scaryguy/6239705 to your computer and use it in GitHub Desktop.
How to reset ID column of a PostgreSQL table to let auto incrementation begin from 1?
# See your sequence name inside psql console with \ds command.
ALTER SEQUENCE seq RESTART WITH 1;
# Update sequence
UPDATE table_name SET id=nextval('seq');
@hipertracker
Copy link

It does not work in Postgres 13

@GarryOne
Copy link

GarryOne commented Mar 11, 2021

PostgreSQL:
Node script: Fix all tables identity: auto-increment / nextval, based on last inserted it.
https://gist.github.com/GarryOne/eabdb5704887c2d1cafd3cb1ebc7fb5e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment