Supabase "new row violates row-level security policy for table": how to fix it
Saving a form, creating a profile or adding a row fails with new row violates row-level security policy for table "...", error code 42501, often as a 403 in the Network tab. Row Level Security (RLS) is on, which is good, but no policy allows this particular write. Don't switch RLS off to make it go away: that opens the table to anyone with your public key. Fix the policy instead.
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €49Step 1: find which write is failing
Open the browser's developer tools, go to the Network tab and repeat the action. The failing request goes to /rest/v1/your_table. Note the method: POST is an insert, PATCH an update, and a POST with Prefer: resolution=merge-duplicates is an upsert. Each needs its own policy. (If the failing upload goes to /storage/v1/, see storage upload fails with 403.)
1. There is no INSERT policy
A table with only a SELECT policy can be read but not written. Inserts are checked with WITH CHECK, not USING. In the Supabase SQL editor, a typical policy for rows owned by the signed-in user looks like this:
create policy "Users can insert their own rows"
on public.your_table for insert
to authenticated
with check ( (select auth.uid()) = user_id );
For updates you need both parts: using decides which rows can be changed and with check decides what they can become.
create policy "Users can update their own rows"
on public.your_table for update
to authenticated
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id );
2. The row doesn't carry the user's id
The policy above only passes if user_id in the new row equals the signed-in user. If your app doesn't send user_id, it is empty and the check fails. Either send it from the app, or better, let the database fill it in:
alter table public.your_table
alter column user_id set default auth.uid();
3. The user isn't actually signed in
If the request goes out without a logged-in session, it runs as the anon role and a policy written to authenticated never applies. Check that the request's Authorization header holds a user token, not just the public key, and that you only call insert after sign-in has finished. Apps that create a profile row straight after signUp hit this when email confirmation is on, because there is no session yet. Create the profile with a database trigger on auth.users instead.
4. insert().select() or upsert needs more than an INSERT policy
Calling .insert(...).select() asks Supabase to return the new row, so a SELECT policy must allow reading it too. An upsert can turn into an update, so it needs INSERT, UPDATE and SELECT policies. If the insert works without .select() but fails with it, this is your cause.
5. The table was created after Supabase's 30 October change
On newer projects, a new table may not be granted to the API roles at all, which gives a similar 42501 "permission denied" error even with correct policies. See permission denied for table (42501) on a new table.
Quick checklist
- Never fix this by disabling RLS
- INSERT policy with
with check, UPDATE policy with bothusingandwith check user_iddefaults toauth.uid()- The request carries a signed-in user's token
- SELECT policy too if you use
.select()or upsert
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €49Still stuck?
Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Get the €49 fix, or start with a free scan.
Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.