duplicate key value violates unique constraint
Last verified:
The fix
Postgres already has a row with this primary key or unique value. The SQLSTATE is 23505. PostgREST returns it as HTTP 409. Inserting the same profile or order again will keep failing.
- 1
Read the constraint name and the Key line
The constraint name is in the message. The detail names the column and the value that already exists. In the Supabase client,
error.codeis"23505"anderror.messageis the Postgres sentence. Do not retry the same insert in a loop. Do not drop the constraint from the Table Editor. - 2
Decide who is allowed to insert that row
If a trigger on
auth.usersalready creates the profile, delete the client insert and select the row. If the client is the only writer, insert once, when the user is created, not on every render of the dashboard. Two writers need one statement that can run twice.upsertwithonConflictset to the constrained column updates the existing row or leaves it, depending on the option. The conflict target has to be a unique index or a primary key.idusually is.const { error } = await supabase.from("profiles").upsert( { id: user.id, email: user.email }, { onConflict: "id" }, ); if (error) throw error; - 3
Treat a retried webhook as already stored
When the unique column is the Stripe event id or the Checkout session id, catch only
23505and return success. The row from the first delivery is the one you keep. Any other code, including42501and23502, is still a failure and should be a non-200 so you can see it. Do not ignore every error from the insert. - 4
Confirm with the value from the detail line
Select the existing row by the column named in
Key (...). You should see one row. Submit the form again. A second submit should not add a row and should not toast the raw constraint name. If you needed a second row, the column in the constraint is the wrong unique key, and the fix is a different column in a new migration, not deletingprofiles_pkey.
Why it happens
Postgres rejects the insert with duplicate key value violates unique constraint "profiles_pkey". The detail line is Key (id)=(…) already exists. The SQLSTATE is 23505. PostgREST's error table maps 23505 to HTTP 409 and forwards that message. The constraint name tells you which rule fired: a primary key (profiles_pkey), a unique column (users_email_key), or a unique pair you added yourself.
Generated sign-up does it with two writers. A trigger on auth.users inserts the profile, and the page also inserts a profile with the same id after signUp. The second insert hits profiles_pkey. The same page on a later visit inserts again instead of reading. Stripe webhook handlers that insert an order on every delivery hit a unique event_id or session_id when Stripe retries. That collision means the first delivery was stored. It is not a failed payment.
This is not Row Level Security. RLS says new row violates row-level security policy. A missing column is PGRST204 or 42703. A missing parent row is foreign key 23503. A null in a NOT NULL column is 23502. Dropping the unique constraint makes the 409 stop and lets the table store two owners for one user.
The next error you'll probably hit
Once this is fixed, the usual follow-up is new row violates row-level security policy. new row violates row-level security policy: RLS is on and no INSERT policy allows this role. Add a policy for the signed-in user. A missing GRANT differs.
Prompt to paste back into your AI tool
Paste this above the error.
Postgres returns 23505: duplicate key value violates unique constraint "CONSTRAINT". Detail: Key (COLUMN)=(value) already exists. PostgREST status is 409 if it came through the Data API. The insert is: <paste the insert or upsert> Do not drop the primary key or the unique constraint. Do not disable RLS. Do not catch every error and continue. Do not insert the same profile from a trigger and from the client. 1. Name the constraint and the column in the Key line, and say whether a trigger also writes this table. 2. If the row is supposed to exist already, stop inserting it on each page load. Select it, or upsert on that conflict target. 3. If this is a webhook retry on a unique event id, handle only code 23505 as already stored and return success. Leave other errors failing. 4. Change only that write.
Common questions
+Sign-up creates the user and then fails on the profile insert. Why?
The auth user was created, and something already inserted a profile with that id. A trigger on auth.users is the usual first writer. The page's insert is the second. Keep one writer, or upsert on id. The login still exists. Only the extra insert failed.
+The code is 23505 and the message says new row violates row-level security. Can both be true?
Those are different responses. 23505 is the unique-constraint sentence. The RLS sentence is new row violates row-level security policy, and it does not use 23505. Handle the code you actually got. A table can hit RLS on one request and a duplicate key on the next, after the policy starts allowing the insert.
+Will onConflict: 'id' work if id is not unique?
No. Upsert needs a unique index or primary key on the conflict target. profiles.id is unique when it is the primary key. A plain column with duplicate values cannot be the conflict target until a unique index exists, and adding that index fails if duplicates are already stored.
Related errors
- new row violates row-level security policynew row violates row-level security policy: RLS is on and no INSERT policy allows this role. Add a policy for the signed-in user. A missing GRANT differs.CursorLovableBoltSupabasePostgresOct 15, 2026
- invalid input syntax for type uuidinvalid input syntax for type uuid. Postgres 22P02. The value is not a uuid. An empty string, "undefined", or "null" is the usual one. Send a real uuid or omit the filter.CursorLovableBoltSupabasePostgresOct 27, 2026
- Could not find the column in the schema cachePostgREST refused a write because that column name is not in its schema cache. The body is PGRST204: Could not find the 'title' column of 'posts' in the schema cache.CursorLovableBoltSupabasePostgresOct 27, 2026
- relation "public.profiles" does not existrelation "public.profiles" does not exist (42P01): that table is missing here. PGRST205 means the schema cache lacks it. Run the migration on this project.CursorLovableBoltSupabasePostgresOct 19, 2026
- Stripe webhook not firingStripe webhook not firing: Checkout can succeed while your server never gets the event. Point the endpoint at the deployed URL and return 200.CursorBoltReplitStripeNext.jsNov 11, 2026