Meaning
PostgreSQL rejected the write because it would create a duplicate key.
Causes
- Inserting a row with an existing unique key
- Concurrent inserts for the same unique value
- Upserts missing proper conflict handling
Fixes
- Use ON CONFLICT to handle duplicates
- Check uniqueness before insert
- Serialize or de-duplicate concurrent writes
Example
ERROR: duplicate key value violates unique constraint "users_email_key" (SQLSTATE 23505)
FAQ
- Is this a data bug?
It usually means a duplicate value is being inserted. - Should I drop the constraint?
Only if duplicates are truly allowed.
Contact
Contact your DBA if you need to change constraints.