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.

What lands in your log

ERROR: duplicate key value violates unique constraint "contacts_email_key"

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

In 10 seconds

What triggers it
Create a table with a unique column, for example email.
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.

Fix

What to do right now

Application-level steps for 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, '[email protected]')
  ON CONFLICT (email) DO NOTHING;

Connected

Everything this error touches

Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.

Open in the interactive map →

Verification

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

Went further?

Pro unlocks the second lab proof

Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.

FollowSubstackLinkedInnew errors · lab notes · hiring loops