Reset Primary key sequence in Postgres

April 2, 2023

Check the max id

SELECT MAX(id) FROM public.table_name;

Check the next id value

SELECT nextval('public."table_name_id_seq"');

If the next id value is greater than max id

Use the below query to set primary key id sequence of your table to max id incremented by one.

SELECT setval('public."table_name_id_seq"',
  (SELECT MAX(id) FROM public.users)
);

Hope it helps. Thank you.