FixRLSFixRLS
  • RLS Error
  • service_role Key
  • Publishable Key
  • Anon Key
  • MCP Setup
  • Blog
How I Fixed "new row violates row-level security policy" in Supabase
2026/06/06

How I Fixed "new row violates row-level security policy" in Supabase

Fix Supabase new row violates row-level security policy errors: insert and update variants, auth.uid() issues, a worked example, and proof tests.

If you build with Supabase long enough, you will probably see this error:

new row violates row-level security policy

It usually appears at the worst possible time.

Your authentication works.
Your table exists.
Your insert query looks fine.
But Supabase still refuses to write the row.

I kept seeing this error in projects generated with Cursor, Lovable, Bolt, and Claude Code. The pattern was almost always the same: the app was generated quickly, but the authorization model was never fully defined.

This post walks through the mental model I use to debug it, the failure variants behind it, and a complete worked example.


What this error actually means

The error does not mean Supabase is broken.

It usually means:

Row Level Security is enabled, but there is no policy allowing this operation for the current user.

For example, if your table has RLS enabled and you try to insert a row as an authenticated user, Supabase checks whether an INSERT policy allows that specific row.

If no policy matches, the insert fails.

That is the point of RLS.

The database is saying:

I know who you are, but I do not have a rule that says you may create this row.


The mistake I see most often

Many AI-generated apps create a table like this:

create table todos (
  id uuid primary key default gen_random_uuid(),
  user_id uuid not null,
  title text not null,
  created_at timestamptz default now()
);

Then the frontend tries to insert:

await supabase.from("todos").insert({
  title: "Ship the app",
  user_id: user.id
});

But if RLS is enabled and there is no insert policy, Supabase rejects it.

The table structure is valid.
The frontend code is valid.
The missing piece is the RLS policy.


Start with one question: who owns the row?

Before writing SQL, ask:

Who should be allowed to create, read, update, or delete this row?

For a simple personal todo app, the answer is:

The authenticated user may only access rows where user_id = auth.uid().

That gives you the shape of the policy.


A basic insert policy

For user-owned rows, a common insert policy looks like this:

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

The important part is the with check clause. It tells Supabase:

Allow the insert only if the row being inserted belongs to the authenticated user.

If you want a deeper explanation of why WITH CHECK exists and how it differs from USING, I wrote a full breakdown in Supabase RLS: USING vs WITH CHECK.


A matching select policy

If you also want users to read their own rows, add:

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

Now Supabase has two rules:

  • WITH CHECK controls what rows the user may create
  • USING controls what rows the user may read

A common trap: a SELECT policy alone does not allow inserts. Your query can read the table perfectly and still fail on every write. See Supabase RLS insert policies with CHECK, explained for the insert-specific side.


Failure variants that look like the same error

The missing-INSERT-policy case is only the most common shape. These variants produce the same error — or a silently broken app — for different reasons.

UPDATE policy gaps

Updates have two doors, not one: USING decides which existing rows the user may touch, and WITH CHECK decides what the row may look like after the change. Two ways this breaks:

  • Missing UPDATE policy entirely. The update does not error — it silently affects zero rows, because the row is invisible to that user. This looks like a frontend bug.
  • USING without WITH CHECK. The user can edit their own rows, but the moment they change user_id — or your app accidentally overwrites it — the save fails with "new row violates row-level security policy". The message says "new row" because an update is really a new version of the row.

RPC and edge functions

If the failing write happens inside an RPC (Postgres function), check how it runs. A security definer function runs as the function owner, who typically bypasses RLS — hiding the error and skipping your policies. A security invoker function runs as the caller, so if an edge function calls it with the service_role key, auth.uid() is null and every ownership check fails. Pass the user's JWT from the client, or keep privileged writes in clearly scoped server code.

Triggers that write to other tables

A trigger on table A that inserts into table B runs that insert as the original user. If B has RLS and the user has no INSERT policy there, the error names the wrong table while your code only touches A. Give B its own policy, or make the trigger security definer when the write should always be allowed.

Testing with service_role

The service_role key bypasses RLS completely. Test scripts or seed code that use it succeed on every insert, so you conclude the policies are fine while real users still fail. Never verify normal user behavior with service_role.


Auth context cases

Sometimes the policies are correct and the request itself has no identity.

auth.uid() returns null

auth.uid() reads the user ID from the JWT sent with the request. It returns null when:

  • the request uses the anon key with no session
  • the Authorization: Bearer <user_jwt> header is missing or malformed
  • the session token expired and the client silently kept running

A null auth.uid() makes auth.uid() = user_id evaluate to null, which is not true, so the insert is rejected. Log the actual session before touching the policies.

anon vs authenticated

A policy declared to authenticated does not apply to anonymous requests at all. For intentionally public inserts (for example, contact form rows), create a separate policy:

create policy "Anyone can submit feedback"
on feedback
for insert
to anon, authenticated
with check (true);

The row belongs to someone else

Even fully logged in, a WITH CHECK (auth.uid() = user_id) policy rejects an insert where user_id is another user's ID. That is the policy working correctly — the fix is in your application logic, not the database.


Warning: "just disable RLS" is the wrong fix

Warning. Disabling RLS, or switching to the service_role key, makes the error disappear by removing the guardrail instead of defining the rule. Every table that holds user data should keep RLS enabled, and every client-facing write should go through policies — not around them. If you suspect a service-role key leaked into client code, rotate it before repairing policies.

"Just disable it temporarily" has a way of becoming permanent, and by then nobody remembers the table is wide open. The service_role answer is worse: that key bypasses every policy you write, so the app looks fixed while every row is exposed.


A complete worked example

Here is the whole cycle on one table: schema, the failing insert, the fix, and verification.

1. Schema with RLS enabled

create table public.projects (
  id uuid primary key default gen_random_uuid(),
  owner_id uuid not null,
  name text not null,
  created_at timestamptz not null default now()
);

alter table public.projects enable row level security;

2. The failing insert

From the app, as a logged-in user:

const { data, error } = await supabase.from("projects").insert({
  name: "Launch checklist",
  owner_id: user.id,
});

Result:

new row violates row-level security policy

The table exists. The user is real. There is simply no rule allowing the row.

3. The fix

create policy "projects_select_own"
on public.projects
for select
to authenticated
using (
  (select auth.uid()) = owner_id
);

create policy "projects_insert_own"
on public.projects
for insert
to authenticated
with check (
  (select auth.uid()) = owner_id
);

create policy "projects_update_own"
on public.projects
for update
to authenticated
using (
  (select auth.uid()) = owner_id
)
with check (
  (select auth.uid()) = owner_id
);

Replace public.projects and owner_id with your own table and owner column names, and run this in a non-production project first.

4. Verification SQL

Confirm the policies actually landed:

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

Then prove behavior, not just absence of error:

  1. Sign in as User A and insert a row. It should succeed, and only User A's rows come back on select.
  2. Sign out, sign in as User B.
  3. As User B, try to select, update, and delete User A's row. Each should return zero rows or an error — never the data.

One passing user proves nothing. The second user is the test.


FAQ

Why do I get this error even though I am logged in?

Because identity and permission are separate checks. The JWT proves who you are; the policy decides what you may write. A missing INSERT policy, or a WITH CHECK condition the row fails (for example user_id set to someone else's ID), rejects the insert even with a valid session.

I added a SELECT policy. Why does my insert still fail?

Because reads and writes use different clauses. Reading rows uses USING; creating rows uses WITH CHECK. A table can have a perfect read policy and no write policy at all.

Why does the insert work in the Supabase SQL editor but fail from my app?

The SQL editor runs as a privileged database role that bypasses RLS. Your app runs as anon or authenticated, where every policy applies. Always verify fixes through the client with a real user session.

How do I debug which policy is rejecting the row?

List what exists: select policyname, cmd, qual, with_check from pg_policies where tablename = 'your_table'; Then compare with_check against the row you are inserting — column names, and the value of auth.uid() in the failing request.

Is disabling RLS an acceptable quick fix?

No. It removes the protection instead of defining the rule. If a write genuinely should not be governed by RLS, model that explicitly (for example with check (true) for a public submissions table) rather than turning RLS off.


How I verify the fix

Do not stop when the error disappears. Test both sides: the correct user can insert and read the row, and a different user cannot read or update it.

The second test is the one people often forget.

If User A can see User B's data, the app is not fixed.

It is only less noisy.


A small tool I built

I kept running into the same RLS patterns, so I built a small tool called FixRLS.

It does not connect to your Supabase project.
It does not ask for secrets.
It does not scan your database.

It helps generate RLS SQL starting points, AI repair prompts, key placement guidance, and proof-of-fix tests for common Supabase scenarios.

You can try it here:

https://fixrls.dev/new-row-violates-row-level-security-policy


Final thought

The Supabase error message is frustrating, but it is usually a sign that the database is protecting you.

The fix is not to turn security off.

The fix is to clearly define:

  • who owns the row
  • which operation is allowed
  • what condition proves ownership
  • how to test the negative case

That is the difference between "the error went away" and "the app is actually safer."


Related FixRLS page

For this specific issue, use the matching FixRLS page: https://fixrls.dev/new-row-violates-row-level-security-policy

All Posts

Author

avatar for FixRLS
FixRLS

Categories

  • Supabase RLS
What this error actually meansThe mistake I see most oftenStart with one question: who owns the row?A basic insert policyA matching select policyFailure variants that look like the same errorUPDATE policy gapsRPC and edge functionsTriggers that write to other tablesTesting with service_roleAuth context casesauth.uid() returns nullanon vs authenticatedThe row belongs to someone elseWarning: "just disable RLS" is the wrong fixA complete worked example1. Schema with RLS enabled2. The failing insert3. The fix4. Verification SQLFAQHow I verify the fixA small tool I builtFinal thoughtRelated FixRLS page

More Posts

Is It Safe to Expose Your Supabase Anon Key?
Supabase RLSSupabase Keys

Is It Safe to Expose Your Supabase Anon Key?

How to reason about visible Supabase anon keys, RLS policies, and what actually protects user data.

avatar for FixRLS
FixRLS
2026/06/10
Supabase RLS for Team and Organization Apps: A Practical Starting Point
Supabase RLS

Supabase RLS for Team and Organization Apps: A Practical Starting Point

A starting point for Supabase RLS policies in team, organization, workspace, and shared-record apps.

avatar for FixRLS
FixRLS
2026/06/17
Why You Should Never Put Your Supabase `service_role` Key in the Frontend
Supabase Keys

Why You Should Never Put Your Supabase `service_role` Key in the Frontend

Why the Supabase service_role key must stay out of browser code and what to do if it leaked.

avatar for FixRLS
FixRLS
2026/06/09

Newsletter

Join the community

Subscribe to our newsletter for the latest news and updates

FixRLSFixRLS

Copy-paste RLS SQL, key guidance, MCP guardrails, and proof-of-fix tests before launch.

Independent tool. Not affiliated with Supabase. Not a scanner. Do not paste real secrets.

Fix Kit
  • RLS insert error
  • service_role key
  • publishable key
  • anon key exposed
  • Supabase MCP setup
Safety
  • Launch Safety Pack intent
  • Safety boundaries
  • Blog
  • GitHub examples
  • FAQ
Legal
  • Privacy Policy
  • Terms of Service
© 2026 FixRLS. All Rights Reserved.
GitHub