VibeFix

Guide · Supabase

Duplicate key value violates unique constraint (23505) in Supabase

Published by Yosef Dabian, founder of VibeFix

How this guide was made: Written with AI assistance and checked against the sources linked on this page.

Updated

Short answer: "duplicate key value violates unique constraint" (Postgres code 23505) means your insert tried to store a value that must be unique, and a row already holds it. Find the constraint the error names, then fix the cause: a second insert for the same user, an upsert without onConflict, a seed that runs twice, or repeated values in one request. An upsert with onConflict handles it.

Get it fixed: a free check first confirms the problem on your live app, usually in under 10 minutes.

Free. No account needed. It usually takes under 10 minutes.

Want it fixed? For Lovable apps or code on GitHub, our AI agent fixes it in your code for $29 and tests your live app again. If we can't complete your re-check, you get your money back.

What it means

PostgreSQL lists 23505 as unique_violation, in the class "Integrity Constraint Violation". A unique constraint "ensure[s] that the data contained in a column, or a group of columns, is unique among all the rows in the table". A primary key is also unique. When a write would put a second row with the same value in, Postgres rejects it.

Supabase's data API passes this code through. In PostgREST's error table, 23505 maps to HTTP 409 with the description "uniqueness violation". PostgREST forwards PostgreSQL errors, so in the browser Network tab you see status 409 and code 23505 in the response body.

Our note: the error message normally includes the name of the constraint that was hit. Read that name first: it tells you which column holds the duplicate.

Which cause is it?

CauseHow to tellFix
Your app inserts into profiles after sign-up, and a row for that user already existsThe constraint is the profiles primary key. A handle_new_user trigger may already insert the rowRemove one of the two inserts, or upsert
An upsert without onConflictThe constraint is a unique column other than the primary key, such as username or emailPass onConflict with that column
A seed file runs over rows that are already thereThe error comes from the seed run, on a database that has dataMake the seed insert with ON CONFLICT DO NOTHING
One request carries the same value twiceA bulk insert whose array repeats an id or another unique valueRemove the repeat from the array

Your app inserts into profiles after sign-up, and the row already exists

Supabase's guide to managing user data shows a trigger that inserts the profile row for you:

create function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = ''
as $$
begin
  insert into public.profiles (id, first_name, last_name)
  values (new.id, new.raw_user_meta_data ->> 'first_name', new.raw_user_meta_data ->> 'last_name');
  return new;
end;
$$;

create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_user();

If this trigger exists and your app code also inserts a profiles row after signUp, the second insert hits the primary key (id) and fails with 23505.

  1. In the SQL Editor, check whether the trigger exists:
   select tgname from pg_trigger where tgname = 'on_auth_user_created';
   
  1. If it returns a row, delete the profiles insert from your sign-up code. The trigger already creates the row.
  2. Our suggestion: if you want the app to own the insert, drop the trigger instead and keep the app insert, after you check that nothing else relies on the trigger.
  3. If you need both to be safe, make the app write an upsert on the primary key:
   const { error } = await supabase
     .from('profiles')
     .upsert({ id: user.id, first_name: 'Ada' })
   
  1. Sign up with a new test address and confirm the profile row appears once.

One more point from the same guide: "If the trigger fails, it could block signups, so test your code thoroughly." A failing trigger shows up as a different error. See Database error saving new user.

An upsert without onConflict

PostgREST says that by default "upsert operates based on the primary key columns". If the clash is on another unique column, Postgres raises 23505 instead of updating the row. The Supabase reference shows how to name the column:

const { data, error } = await supabase
  .from('users')
  .upsert({ username: 'supabot' }, { onConflict: 'username' })
  1. Read the constraint name in the error and find its column. In the SQL Editor:
   select indexname, indexdef
   from pg_indexes
   where schemaname = 'public' and tablename = 'users';
   
  1. Pass that column in onConflict. The column must have a unique constraint or unique index. PostgREST: by specifying on_conflict, "you can make upsert work on a column(s) that has a UNIQUE constraint".
  2. If you want to keep the existing row and skip the new one, the Supabase upsert reference names an ignoreDuplicates option for that case. It gives no further detail, so read the reference before you use it.
  3. Run the upsert twice with the same value. The second run updates the row and raises no error.

A seed file runs over rows that are already there

Supabase's local development guide says seed files "are executed the first time you run supabase start and every time you run supabase db reset", after all migrations. A seed that inserts fixed ids fails with 23505 if it runs against rows that already exist, Our note: the same happens if you paste the seed into the SQL Editor on a database that already holds the data.

PostgreSQL's INSERT documentation describes the fix: ON CONFLICT DO NOTHING "simply avoids inserting a row".

  1. Open supabase/seed.sql.
  2. Add ON CONFLICT to each insert that uses a fixed id or unique value:
   insert into public.profiles (id, first_name)
   values ('11111111-1111-1111-1111-111111111111', 'Ada')
   on conflict (id) do nothing;
   
  1. To refresh the row instead, use DO UPDATE. In PostgreSQL, DO UPDATE needs a conflict target:
   insert into public.profiles (id, first_name)
   values ('11111111-1111-1111-1111-111111111111', 'Ada')
   on conflict (id) do update set first_name = excluded.first_name;
   
  1. Run the seed twice and confirm the second run finishes without an error.

One request carries the same value twice

A bulk insert sends an array of rows in one statement. If two rows in the array carry the same unique value, the second one clashes with the first. The Supabase insert reference shows a bulk example where both rows use id: 1:

const { error } = await supabase
  .from('countries')
  .insert([
    { id: 1, name: 'Mordor' },
    { id: 1, name: 'The Shire' },
  ])
  1. Look at the array your code sends. Check the unique column for repeats.
  2. Remove the repeat, or give each row its own value.
  3. With a bulk upsert, PostgreSQL does not allow one statement to touch the same row twice: it raises a cardinality violation error, and rows proposed for insertion "should not duplicate each other" on the constrained columns. Remove the repeat there too.

How do I check it's fixed?

  select count(*) from public.profiles where id = '<the value>';
  

FAQ

What does error code 23505 mean?

It is PostgreSQL's unique_violation: a write would break a unique constraint or primary key. PostgREST turns it into HTTP 409 with the description "uniqueness violation".

Does a unique constraint stop duplicate rows that contain NULL?

Not by default. PostgreSQL says "by default, two null values are not considered equal", so rows with NULL in a constrained column can both be stored. The NULLS NOT DISTINCT clause changes that.

Should I use upsert or insert?

Use insert when a row must be new, so a duplicate shows up as an error. Use upsert when the row may already exist and you want it updated. An upsert only works on the column you name in onConflict, or the primary key if you name none.

Why does the profile insert fail only for new users?

For a new sign-up, a trigger such as handle_new_user may have already created the profile row a moment earlier. Your app's own insert then clashes with it on the primary key.

Can VibeFix fix it for me?

Yes, for any app whose code is on GitHub, whichever tool built it (Lovable, Bolt, Base44, Replit, v0, Cursor). The free check confirms the problem on your live app first. If you want it fixed, our AI agent finds the cause, fixes it in your code for $29 and tests your live app again. See pricing.

Related: Database error saving new user · insert fails despite WITH CHECK (true) · relation does not exist (42P01)

Sources

Last checked: 24 November 2026.

VibeFix is independent. Supabase is a trademark of its owner.

Want it fixed for you? For any app whose code is on GitHub, whichever tool built it, VibeFix's AI agent can find the cause of this error, fix it in your code and re-check your live app. Start with a free check: https://vibe-fixer.com

More guides · Shopify store accessibility · About · Your privacy choices