Dev.to Security 🔐 Cybersecurity 👁 0 📖 3 min read

Supabase 23505 "duplicate key value violates unique constraint": 6 causes and the safe fixes

Your Supabase insert or signup fails with: {"code":"23505","message":"duplicate key value violates unique constraint \"profiles_pkey\""} You may also see a 409 Conflict in the browser. It often shows up on first

Your Supabase insert or signup fails with:

{"code":"23505","message":"duplicate key value violates unique constraint \"profiles_pkey\""}

You may also see a 409 Conflict in the browser. It often shows up on first login, on retries, or when two requests run at once. The error is plain. The usual fixes are safe, and one popular shortcut opens a security hole.

What it means

A column or set of columns has a unique constraint (a primary key or a unique index), and your query tried to add a row with a value that already exists. Postgres rejects it. The constraint name in the message tells you which one: profiles_pkey means the primary key, users_email_key means a unique email column.

The 6 usual causes

  1. A double submit. The user double-clicks, or the effect runs twice in development, and the second insert collides with the first.
  2. A signup trigger plus app code. A database trigger creates the profile row on signup, and your code inserts it again.
  3. A retry without idempotence. A failed request is repeated after the first one actually succeeded.
  4. Seed or import data that already contains the ids or emails.
  5. A sequence out of sync after you imported rows with explicit ids, so the next generated id already exists.
  6. A race. Two requests check "does it exist?" and both see no, then both insert.

The fix, in order

  1. Read the constraint name to see which column collides.
  2. Use an upsert when "create or update" is what you mean: supabase.from("profiles").upsert({...}, { onConflict: "id" }).
  3. Disable the button while the request runs and make the call idempotent.
  4. If a trigger already creates the row, remove the insert from your app code or switch it to an update.
  5. After a bulk import, reset the sequence: select setval(pg_get_serial_sequence('public.items','id'), (select max(id) from public.items));
  6. Treat 23505 as an expected case and show a clear message, such as "this email is already registered".

The shortcuts that cause trouble

  • Do not drop the unique constraint to make the error go away. You then get duplicate rows, broken joins and wrong totals.
  • Do not use the service_role key to force the write. That key bypasses row-level security and must stay off the client. If it appears in your frontend bundle, rotate it.
  • Do not reveal whether an email exists with a very specific message if you want to avoid account enumeration. Use a general message on public forms.

Keep the policy tight

An upsert needs both an insert and an update policy for the same rows:

create policy "own profile insert" on public.profiles
  for insert to authenticated with check (id = auth.uid());
create policy "own profile update" on public.profiles
  for update to authenticated using (id = auth.uid()) with check (id = auth.uid());

Check what your site exposes

After fixing it, test from the outside with only the public anon key: you should not be able to read or write other users' rows. The free read-only check at https://proofforge.live looks at a deployed site for open Supabase data, leaked keys and exposed files, and lists what it could not test. For user-to-user access, use the free two-account test: https://proofforge.live/two-account-test

Paid one-time fix reports are $9, $29 and $79, and the free scan does not require them.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.