Guide · Supabase
Duplicate key value violates unique constraint (23505) in Supabase
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.
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?
| Cause | How to tell | Fix |
|---|---|---|
Your app inserts into profiles after sign-up, and a row for that user already exists | The constraint is the profiles primary key. A handle_new_user trigger may already insert the row | Remove one of the two inserts, or upsert |
An upsert without onConflict | The constraint is a unique column other than the primary key, such as username or email | Pass onConflict with that column |
| A seed file runs over rows that are already there | The error comes from the seed run, on a database that has data | Make the seed insert with ON CONFLICT DO NOTHING |
| One request carries the same value twice | A bulk insert whose array repeats an id or another unique value | Remove 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.
- In the SQL Editor, check whether the trigger exists:
select tgname from pg_trigger where tgname = 'on_auth_user_created';
- If it returns a row, delete the
profilesinsert from your sign-up code. The trigger already creates the row. - 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.
- 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' })
- 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' })
- 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';
- Pass that column in
onConflict. The column must have a unique constraint or unique index. PostgREST: by specifyingon_conflict, "you can make upsert work on a column(s) that has a UNIQUE constraint". - If you want to keep the existing row and skip the new one, the Supabase upsert reference names an
ignoreDuplicatesoption for that case. It gives no further detail, so read the reference before you use it. - 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".
- Open
supabase/seed.sql. - Add
ON CONFLICTto 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;
- To refresh the row instead, use
DO UPDATE. In PostgreSQL,DO UPDATEneeds 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;
- 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' },
])
- Look at the array your code sends. Check the unique column for repeats.
- Remove the repeat, or give each row its own value.
- 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?
- Run the same action again (sign up, run the upsert, run the seed). The request returns without a 409 and
errorisnull. - In the SQL Editor, count rows for the value that clashed. It returns 1:
select count(*) from public.profiles where id = '<the value>';
- Run the action a second time. A fixed upsert or seed finishes without an error on the second run.
- In the Network tab, the response status is no longer 409.
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
- <https://www.postgresql.org/docs/current/errcodes-appendix.html>
- <https://www.postgresql.org/docs/current/ddl-constraints.html>
- <https://www.postgresql.org/docs/current/sql-insert.html>
- <https://docs.postgrest.org/en/stable/references/errors.html>
- <https://docs.postgrest.org/en/stable/references/api/tables_views.html>
- <https://supabase.com/docs/reference/javascript/upsert>
- <https://supabase.com/docs/reference/javascript/insert>
- <https://supabase.com/docs/guides/auth/managing-user-data>
- <https://supabase.com/docs/guides/local-development/seeding-your-database>
- <https://supabase.com/docs/guides/api/rest/postgrest-error-codes>
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