Vibe-Code Rescue

new row violates row-level security policy for table "profiles"

Why it happens

Row level security is switched on for the table, and no policy lets this request insert this row. Supabase then refuses the insert and returns this error. The table name in your message will differ from the one above.

The usual reasons are a missing insert policy, a policy whose check does not match the row you send, or a request that is not signed in and runs as the anon role.

How to fix it

  1. Look at the policies that exist on the table. Run this in the SQL Editor and swap in your table name.
    select policyname, cmd, roles, with_check
    from pg_policies
    where tablename = 'profiles';
  2. Check that the request is signed in. If this returns no user, the insert runs as anon and a policy written for authenticated users will never match.
    const { data: { user } } = await supabase.auth.getUser()
    console.log(user?.id)
  3. Add an insert policy. The with check clause is what a new row has to satisfy. This one lets a signed-in user create only their own row.
    create policy "Users can create their own profile."
    on profiles for insert
    to authenticated
    with check ( (select auth.uid()) = user_id );
  4. Make the insert send the matching value. If the policy compares user_id to the signed-in user, your code has to set it.
    await supabase.from('profiles').insert({ user_id: user.id, avatar_url })
  5. If the policy looks right and the insert still fails, check the table grants. Supabase says a missing grant raises a 42501 error before any policy runs.
    grant insert on table public.profiles to authenticated;

Do not fix this by putting the service role key in your front end code, and do not switch row level security off. Supabase says a secret key must never be used in the browser. Either shortcut makes the table readable and writable by anyone who finds your project URL.

When it is not this

Questions

Why does Supabase say new row violates row-level security policy?

Row level security is on for the table and no policy allows this insert. Add an insert policy with a with check clause that matches the row you send, and make sure the request is signed in.

Is it safe to use the service role key to get past it?

No. Supabase documents the secret key as server-side only. Never ship it in browser code.

Sources

Still broken?

The free health check asks ten questions about your app and shows the three fixes to do first. It runs in your browser.

If you would rather have someone read the code, the fixed-price audit is $497 one-time. It covers security and scaling, with a written report ranked by severity.