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.

Short answer: Row Level Security (RLS) is on for that table, and no policy lets this user add this row. The database is doing its job. The fix is to add or correct an 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:

  1. There is no INSERT policy at all. Many AI-built apps get a SELECT policy (so reading works), but nobody adds one for inserts.
  2. The WITH CHECK condition is false for the row. The classic case: the policy says auth.uid() = user_id, but the app doesn't send user_id, or sends a different value.
  3. The user isn't signed in yet. The request runs as the anon role, so a policy written to authenticated doesn't apply. This often happens when the insert runs before the login session has loaded.
  4. You insert and then read the row back. In supabase-js, .insert(...).select() also needs a SELECT policy that lets the user see the new row (PostgreSQL docs).
  5. You use upsert. .upsert(...) can insert or update, so it needs UPDATE and SELECT policies as well as INSERT.
  6. 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 suggestsWhat it really does
alter table ... disable row level securityAnyone 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 appThat 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 anonGives 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

  1. Open your live app in a private window, logged out, and make sure you can't see or add other people's data.
  2. Sign in as two different test accounts. Make sure account B can't read or change account A's rows.
  3. In Supabase, open Advisors, then Security Advisor, and fix anything it lists, such as tables with RLS turned off.
  4. Scan the live URL. PatchPaste checks what every visitor's browser gets: secret keys in your JavaScript (including a service_role key an AI may have added), common Supabase tables readable with the public key, exposed /.env, /.git and source maps, and missing security headers. It can't see pages behind a login, so do steps 1 to 3 as well.

Related guides

“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.

Free. No login. No code access.

Want to see one first? View a sample report.