Resetting PostgreSQL sequence

sweemeng
2 min readFeb 19, 2023

One afternoon one of the developers in the team was unable to insert records into the staging database. It raised the following error, on the primary key.

ERROR:  duplicate key violates unique constraint

On top of my head, the reason this happens is because the developer inserted a row with a primary key set.

As I look at the script. The primary key field is not set in the query. It makes sense because the id field is also associated with a sequence. The user doesn’t need to define the primary when inserting it into the table.

--

--

sweemeng

Codemonkey coding for tea. Startup developer at day, community organizer at night. I also happens to be on patreon https://www.patreon.com/sweemeng