Common SQL Errors

PostgreSQL unique_violation Error 23505 Explained and Fixed

August 4, 2025

Error 23505 (unique_violation) appears when an INSERT or UPDATE tries to create a duplicate value in a column or index defined as UNIQUE.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is PostgreSQL error code 23505 unique_violation?

PostgreSQL Error 23505 – unique_violation – means the row you are inserting or updating would duplicate a value in a UNIQUE or PRIMARY KEY index. Identify the conflicting key with the error detail, then either change the data, remove the duplicate row, or use ON CONFLICT handling to upsert.

Error Highlights

Typical Error Message

PostgreSQL Error 23505

Error Type

Constraint Error

Language

PostgreSQL

Symbol

unique_violation

Error Code

23505

SQL State

Explanation

Table of Contents

What is PostgreSQL unique_violation error 23505 and how do I fix it?

PostgreSQL raises SQLSTATE 23505 when a statement would insert or update a row whose key already exists in a UNIQUE index or PRIMARY KEY. The engine rejects the write to protect data integrity.

The error stops the transaction at the failing statement, so downstream queries will not run until the conflict is resolved.

Fixing it quickly keeps applications available and prevents partial data writes.

What Causes This Error?

Unique indexes guarantee that each stored value or combination of values appears only once. An INSERT with a duplicate primary-key value, or an UPDATE that sets a column to an existing unique value, triggers the violation.

Concurrent transactions can also collide.

If two sessions insert the same key before either commits, the second commit fails with 23505.

How to Fix PostgreSQL unique_violation

First read the DETAIL line in the error message to find the conflicting key. Modify the incoming data so the key is unique or delete/merge the existing row.

Use INSERT ... ON CONFLICT ... DO UPDATE (UPSERT) to merge duplicates automatically.

For bulk loads, stage data in a temp table, deduplicate, then insert.

Common Scenarios and Solutions

Failed user signup because email address column is UNIQUE. Solution – check existence before insert or use ON CONFLICT DO NOTHING.

ETL job reruns nightly and re-inserts primary keys already present. Solution – truncate staging table or run ON CONFLICT DO UPDATE.

Best Practices to Avoid This Error

Validate data in application code or database constraints before writing. Wrap INSERTS in ON CONFLICT logic.

Use SERIAL/IDENTITY or UUID default generators for primary keys to avoid manual duplication.

When bulk loading, isolate into a staging area, add DISTINCT, and run dedup queries before merging.

Related Errors and Solutions

Error 23503 – foreign_key_violation – appears when a referenced key is missing. Fix by inserting the parent row first or enabling DEFERRABLE constraints.

Error 23514 – check_violation – fires when CHECK constraints fail. Review the CHECK expression and adjust data accordingly.

.

Common Causes

Related Errors

FAQs

How do I see which column caused unique_violation?

The DETAIL section of the error message shows the exact column values that duplicated. Query the table with those values to locate the row.

Can I ignore duplicate inserts in PostgreSQL?

Yes. Use INSERT ... ON CONFLICT DO NOTHING to silently skip rows that would violate uniqueness.

Is ON CONFLICT slower than plain INSERT?

ON CONFLICT adds a small overhead for conflict checking but usually outperforms retry loops in application code.

How does Galaxy help avoid 23505 errors?

Galaxy's AI copilot suggests UPSERT patterns and highlights unique indexes while you type, reducing accidental duplicate inserts.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo