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
- A double submit. The user double-clicks, or the effect runs twice in development, and the second insert collides with the first.
- A signup trigger plus app code. A database trigger creates the profile row on signup, and your code inserts it again.
- A retry without idempotence. A failed request is repeated after the first one actually succeeded.
- Seed or import data that already contains the ids or emails.
- A sequence out of sync after you imported rows with explicit ids, so the next generated id already exists.
- A race. Two requests check "does it exist?" and both see no, then both insert.
The fix, in order
- Read the constraint name to see which column collides.
- Use an upsert when "create or update" is what you mean:
supabase.from("profiles").upsert({...}, { onConflict: "id" }). - Disable the button while the request runs and make the call idempotent.
- If a trigger already creates the row, remove the insert from your app code or switch it to an update.
- After a bulk import, reset the sequence:
select setval(pg_get_serial_sequence('public.items','id'), (select max(id) from public.items)); - 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_rolekey 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.

