# Duplicate key value violates unique constraint (23505) in Supabase

Source: https://vibe-fixer.com/guides/supabase-duplicate-key-value-violates-unique-constraint
Last updated: 2026-11-24

**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.

Something broke on your live app? Tell us what happened. Our AI agent tries it on your live app and sends you a screenshot, free. [Check my app free](https://vibe-fixer.com/?src=guide-supabase-duplicate-key-value-v-t#triageForm)

## 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:

```sql
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:

```sql
   select tgname from pg_trigger where tgname = 'on_auth_user_created';
   ```

2. If it returns a row, delete the `profiles` insert from your sign-up code. The trigger already creates the row.
3. 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.
4. If you need both to be safe, make the app write an upsert on the primary key:

```js
   const { error } = await supabase
     .from('profiles')
     .upsert({ id: user.id, first_name: 'Ada' })
   ```

5. 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](https://vibe-fixer.com/guides/supabase-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:

```js
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:

```sql
   select indexname, indexdef
   from pg_indexes
   where schemaname = 'public' and tablename = 'users';
   ```

2. 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".
3. 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.
4. 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:

```sql
   insert into public.profiles (id, first_name)
   values ('11111111-1111-1111-1111-111111111111', 'Ada')
   on conflict (id) do nothing;
   ```

3. To refresh the row instead, use `DO UPDATE`. In PostgreSQL, `DO UPDATE` needs a conflict target:

```sql
   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;
   ```

4. 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`:

```js
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?

- Run the same action again (sign up, run the upsert, run the seed). The request returns without a 409 and `error` is `null`.
- In the SQL Editor, count rows for the value that clashed. It returns 1:

```sql
  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](https://vibe-fixer.com/pricing).

Related: [Database error saving new user](https://vibe-fixer.com/guides/supabase-database-error-saving-new-user) · [insert fails despite WITH CHECK (true)](https://vibe-fixer.com/guides/supabase-rls-insert-fails-despite-with-check-true) · [relation does not exist (42P01)](https://vibe-fixer.com/guides/supabase-42p01-relation-does-not-exist)

## Want it fixed?

VibeFix's free check tries the problem you describe on your live app. Tell us what broke, and our AI agent tries it and sends you a screenshot. If you want it fixed, it finds the cause, fixes it in your code and re-checks your live app. The free check never changes your app. [Check my app free](https://vibe-fixer.com/?src=guide-supabase-duplicate-key-value-v-f#triageForm)

## 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
