Hi, I’m Dung Huynh Duc . Nice to meet you.

About Me

I’m a full stack developer. I’m a fast learner and self-taught coder. I often take my time for researching and learning about hot and trending technology.

blog_hero_TIL #21 - How to fix PostgreSQL duplicate key violates (out of sync)
PostgreSQL.
db

TIL #21 - How to fix PostgreSQL duplicate key violates (out of sync)

Dung Huynh

Thu Oct 21 2021

Why

For some reason, we could have an issue with the primary key auto-generated.

How

Check the next primary key value

SELECT nextval('public.source_id_seq'), MAX(id) FROM "source";

Then adjust the value

 SELECT SETVAL((SELECT PG_GET_SERIAL_SEQUENCE('"source"', 'id')), (SELECT (MAX("id") + 1) FROM "source"), FALSE);