Deutsch
Error messageSupabase · RLS

Fix “new row violates row-level security policy” in Supabase

Why Supabase says new row violates row-level security policy on insert, update or upsert, the six usual causes and the fix that keeps your data private.

The message you seePostgres · 42501

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

By Tim GeithnerPublished 4 min read

Short answer

Postgres refused to write a row because no row level security policy allows it for the current user. In apps built with Lovable, Bolt or Cursor the cause is almost always one of six things: no insert policy, the owner column left empty, a user id that is not the caller’s, a signed-out request, .insert().select() without a select policy, or an upsert without an update policy. Fix the policy or the request. Never fix it by turning RLS off or by using the service role key in the browser.

Ask an AI about this page:ChatGPTClaude

On this page

Postgres checks every new or changed row against the table’s row level security policies. If none of them allows the row for the current user, the write is refused with error code 42501 and this message. Nothing was saved. The good news: the database did its job. The bad news: one of your policies, or the request your AI builder wrote, does not say what you think it says.

Find your cause in 30 seconds

What you didMost likely cause
Insert, and the table has policies only for selectNo insert policy
Insert without sending user_idThe owner column is empty
Insert or update with a user_id from the request bodyThe id is not the caller’s
Insert right after page load, or from a public formThe request is signed out
.insert(...).select() or .single() after the insertNo select policy for the new row
.upsert(...), message ends in (USING expression)No update policy

All six reproduce on a plain table of notes with an owner check. We ran each one against Postgres with Supabase’s roles before writing this page:

The table used on this page
create table public.notes (
  id uuid primary key default gen_random_uuid(),
  user_id uuid not null references auth.users (id) on delete cascade,
  body text not null,
  created_at timestamptz not null default now()
);

alter table public.notes enable row level security;

The six causes

1. There is no insert policy

With RLS on, everything is denied until a policy allows it. Many generated migrations add a select policy and forget insert, so reading works and saving fails. A table with RLS on and no policies at all fails the same way.

2. The owner column is empty

The insert policy compares auth.uid() with user_id. If the request does not send user_id and the column has no default, the value is null, and null = auth.uid() is never true. This is the most common cause in Lovable apps: the form sends body and nothing else.

3. The row names another user

The request sends a user_id that is not the caller’s: a hard-coded test id, an id from the URL, or the id of the person being invited. The policy rightly blocks it. If you need users to create rows for others (invites, shared workspaces), that is a team pattern, not an owner pattern; the RLS policy builder generates both.

4. The request is signed out

Without a session, the request runs as the anon role and auth.uid() is null. Typical triggers: the insert runs before supabase-js has restored the session after a page load, the session expired, or the call happens on a public page. Wait for the session (supabase.auth.getSession() or the auth state listener) before writing.

5. The request reads the row back

supabase.from("notes").insert(row).select() asks Postgres to return the new row. Postgres then also checks the select policies against it, and fails the whole insert if the user may not see the row. An insert policy alone is not enough when the code reads back what it wrote.

6. An upsert hits an existing row

.upsert() becomes insert ... on conflict do update. When the row already exists, Postgres needs an update policy. Without a select policy you get the plain message; with a select policy but no update policy, the message gains (USING expression). An update that moves a row to another owner fails too, because the update policy’s check applies to the new version of the row.

How to fix it

Give the table one policy per action, each tied to the caller, and let the database fill in the owner. This covers causes 1, 2, 5 and 6 at once:

SafeFix: owner policies and a default owner
alter table public.notes alter column user_id set default auth.uid();

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

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

create policy "Users can change their notes"
  on public.notes for update
  to authenticated
  using ((select auth.uid()) = user_id)
  with check ((select auth.uid()) = user_id);

create policy "Users can delete their notes"
  on public.notes for delete
  to authenticated
  using ((select auth.uid()) = user_id);

Then the app no longer needs to send the owner at all, and cannot send a wrong one by mistake:

SafeThe insert in your app
const { data, error } = await supabase
  .from("notes")
  .insert({ body })
  .select()
  .single();

For cause 3, remove user_id from what the client sends, or check that it equals the signed-in user. For cause 4, make sure a user is signed in before the write; if a public form must write, give it its own narrow insert policy for the anon role on a table that holds nothing private.

Fix the RLS insert error without weakening security

My Supabase app fails with “new row violates row-level security policy for table your_table”. Find the table’s policies in the migrations and the code that writes to it. Then fix it like this, in a new migration: make the owner column default to auth.uid(), and make sure there is one policy each for select, insert, update and delete, for the authenticated role only, that allows the row only when (select auth.uid()) = the owner column (update needs both USING and WITH CHECK). In the app code, stop sending the owner column from the client and make sure the write only runs when a user is signed in.

Do not disable RLS, do not add policies with true, do not use the service role key in client code and do not edit existing migrations. Show me the migration and the code change before applying them.

Check that the fix works

In the SQL Editor, act as a signed-in user inside a transaction. Use a real user id from Authentication → Users:

Check: insert as a user
begin;
set local role authenticated;
set local request.jwt.claims = '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated"}';
insert into public.notes (body) values ('Hello') returning id, user_id;
rollback;

The insert returns one row whose user_id is that id. Then change the insert to values ('Hello', '22222222-2222-2222-2222-222222222222') with (body, user_id) and another user’s id: it must still fail with this error. That failure is the policy protecting other users. The guide to testing RLS policies has the full set of checks to run before launch.

Frequently asked questions

Should I just disable RLS to make the error go away?

No. With RLS off, anyone who has your public anon key, which ships in every browser bundle, can read and change every row of the table. The error means the database is protecting you; the fix is a policy that allows exactly the rows the user may write.

Why does the insert work in the SQL editor but not in my app?

The SQL editor runs as the database owner, and RLS does not apply to the owner. Your app runs as the anon or authenticated role, so the policies decide. Test as a user, as shown under Check that the fix works.

What does the variant with (USING expression) mean?

You ran an upsert, or an insert with on conflict do update, and the row already exists. Postgres then needs an update policy whose USING clause lets the user see and change that existing row. Add an update policy with the same owner check.

My policy looks right. How do I find out which check fails?

Run the insert inside a transaction as the user, as shown on this page, and try each cause in turn: is auth.uid() set, does the owner column hold that id, does a select policy cover the new row if the request returns it, and does an update policy exist for upserts.

Sources

  1. 1.Row Security Policies — PostgreSQL Documentation
  2. 2.CREATE POLICY — PostgreSQL Documentation
  3. 3.Row Level Security — Supabase Docs
  4. 4.JavaScript: Upsert data — Supabase Docs

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt