Skip to main content
AllDevToolsHub
Back to error codes
ERROR 23505

PostgreSQL Unique Constraint Violation

Database Error

An INSERT or UPDATE attempted to create a duplicate value in a column with a UNIQUE constraint.

Root Cause

Trying to insert a row with a value that already exists in a unique-indexed column (e.g., duplicate email, duplicate username).

How to Fix

Use INSERT ... ON CONFLICT DO NOTHING or ON CONFLICT DO UPDATE to handle duplicates gracefully. Or check for existence before inserting.

Quick Summary

PostgreSQL error 23505: unique constraint violation. Handle with ON CONFLICT DO NOTHING or ON CONFLICT DO UPDATE. Return a 409 Conflict HTTP status to the client.

Key Takeaways

Key Takeaways

  • Error code 23505 = unique_violation in PostgreSQL
  • Handle with ON CONFLICT: INSERT ... ON CONFLICT (email) DO NOTHING
  • Return HTTP 409 Conflict to the client
  • The error detail includes the constraint name and duplicate value
Use Cases

When to use it

  • Duplicate email registration
  • Duplicate username
  • Duplicate order ID
Watch out

Common Mistakes

  • Returning 500 instead of 409 for unique constraint violations
  • Not handling the error gracefully, always catch and return a meaningful error message
FAQ

23505 PostgreSQL Unique Constraint Violation, Frequently Asked

How do I get the duplicate value from the error?

In Node.js with pg: error.detail contains the duplicate key value. Parse it to show a user-friendly message.

Still having issues?

Check your network logs or use our developer tools to inspect headers, decode tokens, or validate your requests.