Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GodziLaravel's avatar

Postgresql generate an already used PK value!

Hello;

I'm using Postgesql, when I add a new row to the table permissions it seems like Postgresql generate an exisiting id !

the last Id is 104, normally when I add a new record the Id should be 105 but I got this error message :

Illuminate\Database\QueryException with message 'SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates unique constraint "permissions_pkey"
DETAIL:  Key (id)=(94) already exists. (SQL: insert into "permissions" ("name", "short_description", "description", "slug", "updated_at", "created_at") values (...) returning "id")'

The id is not updated !

About psql version

psql (PostgreSQL) 13.1 (Ubuntu 13.1-1.pgdg18.04+1)
0 likes
1 reply
trogers1884@gmail.com's avatar

You need to update the sequence current value property in your postgres schema. The most likely cause is that you added records with the id value instead of letting the sequencer handle it. When you bypass the auto-increment and implicitly add that value, that property is not updated.

This is handled very easily in PgAdmin but can also be done with SQL: https://www.postgresql.org/docs/13/sql-altersequence.html

Please or to participate in this conversation.