PostgreSQL UNIQUE violation (SQLSTATE 23505) Error Guide

Common SQL Errors

Galaxy Team
June 25, 2025
Constraint Error

The error occurs when an INSERT or UPDATE tries to store a duplicate value in a column or index declared as UNIQUE.

PostgreSQL
Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

What is a UNIQUE violation error?

UNIQUE violation (SQLSTATE 23505) happens when an INSERT or UPDATE writes a duplicate value into a UNIQUE-constrained column or index. Remove or alter the duplicate data, or use INSERT … ON CONFLICT to handle collisions, to resolve the error.

Typical Error Message

ERROR: duplicate key value violates unique constraint "<constraint_name>" (SQLSTATE 23505)

Explanation

Table of Contents

What is the UNIQUE violation error in PostgreSQL?

UNIQUE violation, SQLSTATE 23505, signals that a row being inserted or updated duplicates a value protected by a UNIQUE constraint or unique index.

The server aborts the statement and, inside a transaction, marks the entire transaction as failed until you ROLLBACK or use a savepoint.

What Causes This Error?

The most common trigger is inserting a row whose primary-key or unique-key value already exists in the target table.

Updating a non-unique row so that its new value collides with another row covered by the same UNIQUE constraint also raises the error.

How to Fix a UNIQUE violation

First, identify which column or index is named in the error message.

Query pg_constraint or pg_indexes for details.

Next, locate the conflicting data with a SELECT filtering on the duplicate key value, then decide whether to delete, modify, or merge records.

Common Scenarios and Solutions

Bulk import scripts often ignore existing keys—use INSERT … ON CONFLICT DO NOTHING to skip duplicates safely.

Upserts require INSERT … ON CONFLICT DO UPDATE to merge incoming data with existing rows.

Best Practices to Avoid This Error

Always create deterministic primary keys (e.g., UUID v4) client-side or with DEFAULT gen_random_uuid() to eliminate race-condition duplicates.

Wrap high-write operations in explicit transactions and add retries with exponential back-off to handle occasional conflicts gracefully.

Related Errors and Solutions

NOT NULL violation (SQLSTATE 23502) arises when NULL is inserted where it is forbidden—set a default value or supply data.

Foreign-key violation (SQLSTATE 23503) happens when referenced rows are missing—insert parent rows first or use DEFERRABLE constraints.

.

Common Causes

Related Errors

FAQs

Does a UNIQUE violation roll back the entire transaction?

Yes. In PostgreSQL, any error inside a transaction marks the transaction as failed; you must ROLLBACK or use a savepoint.

How can I find which row caused the violation?

The error provides the duplicate key value. Query the table filtering on that value to locate the conflicting record.

Can I ignore duplicates automatically?

Use INSERT … ON CONFLICT DO NOTHING to skip rows that would violate the UNIQUE constraint.

Will adding more indexes prevent this error?

No. Additional indexes do not change the UNIQUE constraint’s logic; ensure only one unique index governs the intended columns.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
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