SQLSTATE 23505Severity mediumLab verified

Incident brief

Duplicate key value violates unique constraint

A row was inserted with a value that a unique column already has. PostgreSQL blocked the insert so the unique constraint stays true.

Reproduced on PostgreSQL 16.14Verified 2026-07-15 (Docker lab, PostgreSQL 16.14)Reviewed by Verified against PostgreSQL 16.14 in an isolated lab environment

In 10 seconds

What
Duplicate key value violates unique constraint
What triggers it
Create a table with a unique column, for example email.
The fix
Catch the error and tell the user that value is already taken.
Proof
Reproduced on PostgreSQL 16.14 → The second insert was rejected. Only the first row exists in the table afterward — no duplicate was created.

The fix

What to do right now

The immediate, application-level response to this error.

  • Catch the error and tell the user that value is already taken.
  • Use INSERT ... ON CONFLICT DO NOTHING (or DO UPDATE) instead of a bare INSERT.
  • Keep the unique constraint. Do not replace it with an application-side check.
Fix — SQL
-- instead of a bare INSERT that can raise 23505:
INSERT INTO crm.contacts (id, email) VALUES (2, 'alice@example.com')
  ON CONFLICT (email) DO NOTHING;

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 16.14
Last verified
2026-07-15 (Docker lab, PostgreSQL 16.14)
Reviewed by
Verified against PostgreSQL 16.14 in an isolated lab environment
Audit status
reviewed