PatchPaste · Guides
Last updated 30 October 2026
“new row violates row-level security policy”: what it means and how to fix it safely
Your Lovable or Bolt app won't save a new row, and Supabase shows this error. Here is what each cause means, the policy to add, and the “quick fixes” to say no to.
INSERT policy, not to turn RLS off.The full error usually looks like this (Postgres error code 42501):
new row violates row-level security policy for table "todos"
Why this happens
When RLS is on, Supabase blocks every action on a table unless a policy allows it. An insert is only allowed when an INSERT policy's WITH CHECK condition is true for the new row (Supabase docs). You see this error when:
- There is no
INSERTpolicy at all. Many AI-built apps get aSELECTpolicy (so reading works), but nobody adds one for inserts. - The
WITH CHECKcondition is false for the row. The classic case: the policy saysauth.uid() = user_id, but the app doesn't senduser_id, or sends a different value. - The user isn't signed in yet. The request runs as the
anonrole, so a policy writtento authenticateddoesn't apply. This often happens when the insert runs before the login session has loaded. - You insert and then read the row back. In supabase-js,
.insert(...).select()also needs aSELECTpolicy that lets the user see the new row (PostgreSQL docs). - You use upsert.
.upsert(...)can insert or update, so it needsUPDATEandSELECTpolicies as well asINSERT. - It's a file upload. Supabase Storage uploads are inserts into
storage.objects, so the same error means the bucket is missing an upload policy (Supabase Storage docs).
The safe fix
For a table where each row belongs to one user, two small changes fix most cases.
1. Let the database fill in the owner. Give user_id a default, so the app can't forget it or send the wrong one:
alter table public.todos alter column user_id set default auth.uid();
2. Add an INSERT policy that only lets users create their own rows:
create policy "Users can add their own todos" on public.todos for insert to authenticated with check ( (select auth.uid()) = user_id );
If you read the row back after inserting, make sure a matching SELECT policy exists too:
create policy "Users can read their own todos" on public.todos for select to authenticated using ( (select auth.uid()) = user_id );
Then sign in as a test user and try again. If it still fails, check that the request really has a logged-in session. In DevTools, open the Network tab and look at the request's Authorization: Bearer ... header. It should hold the user's token, not just the anon key.
Fixes to say no to
When you paste this error into an AI builder, it may offer a “quick fix”. These make the error go away by removing your protection:
| The AI suggests | What it really does |
|---|---|
alter table ... disable row level security | Anyone with your public anon key can read and change every row in the table. |
with check (true) | Anyone the policy applies to can insert any row, including rows “owned” by other users. If it isn't limited to authenticated, that includes logged-out visitors. |
Using the service_role key in the app | That key skips RLS completely. If it's in browser code, anyone can copy it and get full access to your database. |
grant all on ... to anon | Gives logged-out visitors table access the app probably never needed. |
A good fix is always narrow: one policy, for one action, for the right role, with a condition that ties the row to the user.
Paste this into Lovable, Bolt or Cursor
Inserts into the todos table fail with "new row violates row-level security policy". Keep RLS on. Set the user_id column default to auth.uid(). Add an INSERT policy for the authenticated role with check ((select auth.uid()) = user_id), and a SELECT policy with the same condition. Do not disable RLS, do not use with check (true), and never use the service_role key in frontend code.
How to check you didn't open anything by accident
- Open your live app in a private window, logged out, and make sure you can't see or add other people's data.
- Sign in as two different test accounts. Make sure account B can't read or change account A's rows.
- In Supabase, open Advisors, then Security Advisor, and fix anything it lists, such as tables with RLS turned off.
- Scan the live URL. PatchPaste checks what every visitor's browser gets: secret keys in your JavaScript (including a
service_rolekey an AI may have added), common Supabase tables readable with the public key, exposed/.env,/.gitand source maps, and missing security headers. It can't see pages behind a login, so do steps 1 to 3 as well.
Related guides
- Supabase 42501 “permission denied for table”: a different error with the same code, caused by missing table grants.
- Supabase RLS using (true): why an always-true policy is the same as no policy.
- Supabase service_role key exposed: what to do if that key ended up in your app.
- Is my Supabase storage bucket public?: upload policies on
storage.objects.
“new row violates” FAQ
What does "new row violates row-level security policy" mean?
Row Level Security (RLS) is on for that table, and no INSERT policy allows this user to add this row. The database is doing its job. The error code is 42501.
Should I turn RLS off to fix it?
No. With RLS off, anyone with your public anon key can read and change every row in the table. Add a narrow INSERT policy instead.
What INSERT policy should I add?
For a table where each row belongs to one user: create policy for insert to authenticated with check ((select auth.uid()) = user_id). Also give user_id a default of auth.uid() so the app can't send the wrong value.
Why does it fail even though I have an INSERT policy?
Common reasons: the app doesn't send user_id or sends a different value, the user isn't signed in yet so the request runs as anon, you call .insert().select() without a SELECT policy, or you use upsert, which also needs UPDATE and SELECT policies.
Is it OK if my AI builder uses the service_role key to fix it?
No. The service_role key skips RLS completely. If it ends up in browser code, anyone can copy it and get full access to your database.
Why do I get this error when uploading a file?
Supabase Storage uploads are inserts into storage.objects. The bucket needs an INSERT policy on storage.objects that allows the upload, ideally limited to the user's own folder.
Check your live app free
Paste your URL to check for leaked secret keys, common Supabase tables strangers can read (at most one row, never stored), exposed files and missing headers. The report is free.
Want to see one first? View a sample report.