Skip to content

All posts

How to Fix "new row violates row-level security policy" in Supabase

Supabase blocked your insert because Row Level Security (RLS) is on and no policy allows this user to write this row. Add an INSERT policy that checks auth.uid() = user_id, and make sure your insert actually sends the logged-in user's ID. Do not turn RLS off.

3 min read

Quick answer: Supabase blocked your insert because Row Level Security (RLS) is on and no policy allows this user to write this row. Add an INSERT policy that checks auth.uid() = user_id, and make sure your insert actually sends the logged-in user's ID. Do not turn RLS off.

If you built your app with Lovable, Bolt, Cursor or Replit, this is the most common database error you will hit. It usually appears the first time a real user tries to save something.

What the error means

The full message looks like this:

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

It comes with Postgres code 42501 and usually an HTTP 403. RLS is a per-row permission system. When it is enabled, every insert is rejected unless a policy says yes. Your AI tool enabled RLS (good) but did not write a matching policy, or wrote one your insert does not satisfy.

The 5 causes, most common first

  1. No INSERT policy exists. RLS is on, the table has zero policies for inserts.
  2. The insert does not include user_id. The policy checks auth.uid() = user_id, but the row arrives with user_id empty.
  3. The user is not logged in when the insert runs. The request goes out as anon, and the policy only allows authenticated.
  4. You insert and read back in one call. .insert().select() also needs a SELECT policy. Without one, the insert fails with this same error.
  5. It is a file upload. Storage uploads insert into storage.objects, which needs its own policy.

Fix it, step by step

Step 1. See what policies exist. Open the Supabase SQL Editor and run:

select policyname, cmd, roles, qual, with_check
from pg_policies
where tablename = 'todos';

No row with cmd = INSERT (or ALL) means cause 1.

Step 2. Add the INSERT policy.

create policy "Users can insert their own rows"
on public.todos
for insert
to authenticated
with check ((select auth.uid()) = user_id);

Step 3. Make user_id fill itself in. This removes cause 2 for good:

alter table public.todos
alter column user_id set default auth.uid();

Step 4. Add a SELECT policy if your code calls .select() after the insert:

create policy "Users can read their own rows"
on public.todos
for select
to authenticated
using ((select auth.uid()) = user_id);

Step 5. Confirm the user is logged in. In the browser console, run await supabase.auth.getUser(). If user is null, the insert is running before login completes. That is an auth bug, not a policy bug.

Replace todos and user_id with your own table and owner column.

What not to do

  • Do not disable RLS. The error disappears, and so does the lock on your data. See Post 4.
  • Do not put the service_role key in your frontend. It bypasses RLS for anyone who opens your site's source.
  • Do not use with check (true) unless the table really is open to every user.

The prompt to give your AI tool

For the table todos, keep RLS enabled. Add INSERT, SELECT, UPDATE and DELETE policies for the authenticated role where auth.uid() = user_id. Set the default of user_id to auth.uid(). Do not disable RLS and do not use the service role key in client code. Show me the SQL before running it.

FAQ

Why did it work in preview and fail in production? In preview you were often the only user, or the tool ran with elevated access. Production uses the public anon key, so every policy is enforced.

I get this error on upsert. Why? An upsert needs INSERT, UPDATE and SELECT policies. Add all three.

I get this error uploading a file. Add a policy on storage.objects for your bucket. Table policies do not cover Storage.

Is error 42501 the same thing? Yes. 42501 is the Postgres code for insufficient privilege. With this message, it is RLS.

Still stuck?

Rayionyx fixes broken AI-built apps. Run the free scan at rayionyx.com to check your app, or book the $199 Rescue Audit and get a full written report in 24 hours.

Can we watch how you use this site?

We use Microsoft Clarity to see where visitors click and scroll, so we can fix confusing pages. It sets cookies and records your visit. Nothing loads unless you say yes.

Read the privacy notice