PostgreSQL Error 23505: Unique Violation
ERROR: duplicate key value violates unique constraint "users_email_key" DETAIL: Key (email)=(john@example.com) already exists.
What is PostgreSQL Error 23505?
PostgreSQL SQLSTATE 23505 occurs when an INSERT or UPDATE violates a UNIQUE or PRIMARY KEY constraint. PostgreSQL's error message names the specific constraint and shows the conflicting key value.
Common Causes
- 1
Inserting a row where a unique-indexed column already has that value
- 2
Concurrent inserts from multiple transactions both trying to insert the same unique value
- 3
Sequence gaps causing ID conflicts in manual ID assignment
- 4
Data migration inserting records that already exist
Step-by-Step Solutions
Use INSERT ... ON CONFLICT DO NOTHING: INSERT INTO users (email) VALUES ('x@x.com') ON CONFLICT (email) DO NOTHING;
Use INSERT ... ON CONFLICT DO UPDATE (upsert): INSERT INTO users (email, name) VALUES ('x@x.com', 'New') ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;
Check existence first: INSERT INTO t SELECT v WHERE NOT EXISTS (SELECT 1 FROM t WHERE col = v)
For sequences: SELECT setval('table_id_seq', (SELECT MAX(id) FROM table));
Prevention Tips
Use ON CONFLICT for all INSERT operations on tables with unique constraints
Use database-generated IDs (SERIAL or GENERATED ALWAYS AS IDENTITY) instead of application-assigned IDs
Implement optimistic locking for concurrent update scenarios
Pre-validate uniqueness in the application layer for better user feedback (but still handle DB constraint)
Frequently Asked Questions
How do I do an upsert in PostgreSQL?
How do I insert if not exists in PostgreSQL?
Related Errors
Still Stuck?
Ask our AI SQL Assistant or the community — get answers in seconds.