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.

PostgreSQL.
db

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

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

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);