Skip to content

PostgreSQL error 23505: duplicate key value violates unique constraint

PostgreSQL rejected a change because its key conflicts with an existing row.

duplicate key value violates unique constraint "users_email_key"
DETAIL: Key (email)=(reader@example.com) already exists.
SQLSTATE: 23505

The constraint, columns, and values depend on your table.

A primary key or unique rule requires distinct keys. For a key with multiple columns, PostgreSQL checks the combination of values. The failed statement attempted to create a duplicate.

  • An INSERT repeats an existing key.
  • The ID sequence is behind the table. Rows were inserted with explicit IDs, or loaded from a dump or import, so the sequence now hands out an ID that a row already uses.
  • An UPDATE changes a key to one another row already uses.
  • An import contains duplicates or repeats a previous import.
  • Concurrent requests try to create the same item.
  1. Read the constraint name and key values in the error detail.
  2. Find the existing row with that key.
  3. Decide whether your input represents a new item or a change to that row.
  4. Correct the input before retrying the operation.

For the example above:

SELECT email
FROM public.users
WHERE email = 'reader@example.com';

Replace the example names and value with those from your error. For a compound key, include each key column in the lookup.

Keep the unique rule while correcting the data. Removing it permits duplicates that other application code may not handle.

The ID is generated and the error says the key already exists

Section titled “The ID is generated and the error says the key already exists”

Suspect the sequence when the failed INSERT did not set the ID, and the error names the primary key, such as users_pkey with Key (id)=(42) already exists. Compare the highest ID in the table with the sequence’s next value:

SELECT max(id) FROM public.users;
SELECT last_value, is_called
FROM public.users_id_seq;

If last_value is lower than the maximum ID, move the sequence past the existing rows:

SELECT setval(
pg_get_serial_sequence('public.users', 'id'),
COALESCE(max(id), 0) + 1,
false
)
FROM public.users;

Replace public.users and id with your table and key column. A sequence is usually named <table>_<column>_seq. The statement needs permission to update the sequence. To avoid the problem again, leave generated ID columns at their database default when you insert rows.

For a grid edit, correct the staged value and review Preview SQL before Apply. For an import, check both duplicate input rows and rows already stored in the destination. See import and export.

These checks apply to local, Docker, and hosted databases. On a company server, ask the table owner which record should keep the key if that decision is unclear.

TableAI uses Duplicate Value for this error and includes the PostgreSQL message and code in the error details. After a failed grid Apply, your proposed edits remain available for correction. See edit rejected to check the reported transaction outcome before retrying.