TraceResolve
Back to CatalogPostgreSQL

PostgreSQL 23505: duplicate key value violates unique constraint

ERROR: duplicate key value violates unique constraint "users_email_key" DETAIL: Key (email)=(john@example.com) already exists.
Immediate Remediation
bash
psql -c "SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'users';" && psql -c "ALTER TABLE users DROP CONSTRAINT users_email_key;"
Root Cause Analysis

An INSERT or UPDATE attempted to store a value in a column that has a unique index, but that value already exists in another row, so PostgreSQL rejects the operation to enforce uniqueness.

Verification & Guardrails

  • Verify the constraint name and table name from the error before dropping; dropping the wrong constraint can break app logic.
  • If the duplicate is expected, use ON CONFLICT (email) DO UPDATE or DO NOTHING in the INSERT statement instead of dropping the constraint.
  • Check for partial unique indexes or case-insensitive collations that may cause false positives.

Have a custom or uncategorized crash?

Run your trace through our in-memory client privacy sandbox for instant SRE remediation.

Open Diagnostic Studio