Duplicate Key Value Violates Unique Constraint in PostgreSQL
What This Error Means
This error occurs when inserting a record with a value that already exists in a unique column.
Example
INSERT INTO users(id, email) VALUES (1, '[email protected]');
If ID 1 already exists, the insert fails.
Fix Options
- Use
ON CONFLICT DO NOTHING - Use
ON CONFLICT DO UPDATE - Validate data before insert