afterbuild/ops
ERR-945/Supabase · RLS
ERR-945
new row violates row-level security policy for table (SQLSTATE 42501)

appears when:After RLS is enabled on a table but no INSERT policy has been written for the authenticated role

Supabase RLS blocking insert

Postgres defaults to deny when RLS is on. Every INSERT, SELECT, UPDATE, DELETE is blocked until you write a policy.

Last updated 4 November 2026 · 6 min read · By Hyder Shah
Direct answer
Supabase RLS blocking insert (42501) means the target table has RLS enabled and the authenticated role has no INSERT policy whose WITH CHECK evaluates true for the row. Create a policy FOR INSERT TO authenticated WITH CHECK (auth.uid() = user_id) and a matching SELECT policy so the chained .select() can read the row back.

How do I fix Supabase RLS blocking insert?

supabase/migrations/insert_policy.sql
sql
01-- Single-tenant ownership pattern (most AI apps start here)02create policy "users insert own rows"03on public.tasks04for insert05to authenticated06with check (auth.uid() = user_id);07 08-- Default auth.uid() so the client cannot forget to set user_id09alter table public.tasks10alter column user_id set default auth.uid();11 12-- Verify in SQL editor13set role authenticated;14insert into public.tasks (title) values ('test') returning *;
Run in Supabase SQL editor — creates the INSERT policy, sets the default, and verifies with a test insert

Not a developer? We fix this for you at a fixed price: $299 emergency triage, done in 48 hours or fully refunded. Not sure what else is broken? Send the repo for the $49 audit.

What if the quick fix doesn't work?

01 · Add the matching SELECT policy

supabase/migrations/select_policy.sql
sql
01create policy "users read own rows"02on public.tasks03for select04to authenticated05using (auth.uid() = user_id);
Without a SELECT policy the chained .select() fails with the same row-level security error

02 · Multi-tenant: join through memberships

supabase/migrations/multi_tenant_policy.sql
sql
01create policy "members insert org rows"02on public.tasks03for insert04to authenticated05with check (06  organization_id in (07    select organization_id from memberships08    where user_id = auth.uid()09  )10);

03 · Write a pgTAP regression test

tests/rls_tasks.sql
sql
01-- tests/rls_tasks.sql — asserts cross-tenant writes fail02begin;03select plan(1);04 05set local role authenticated;06set local "request.jwt.claims" = '{"sub":"user-a-uuid"}';07 08select throws_ok(09  $$insert into public.tasks (user_id, title) values ('user-b-uuid', 'test')$$,10  '42501',11  'cross-user insert must be blocked by RLS'12);13 14select * from finish();15rollback;
Add to CI so the policy cannot regress when someone edits the schema

Upserts, triggers and storage buckets

An upsert (.upsert(), which runs INSERT … ON CONFLICT DO UPDATE) needs more than an INSERT policy. Postgres checks the proposed row against the INSERT policy’s WITH CHECK, checks it against the SELECTpolicies, and if the update path is taken, checks the existing row against the UPDATE policy’s USING and the new row against its WITH CHECK. Unlike a plain UPDATE, a failed check here throws an error rather than skipping the row. So an upsert that fails while a plain insert works usually means a missing SELECT or UPDATE policy.

A trigger that writes to a second table, such as an audit log or a profile row, runs that write as the calling role by default, so the second table needs its own policy. Marking the trigger function security definerruns it with its owner’s rights instead; Supabase’s docs note that such a function only skips RLS when its owner can. Keep those functions out of schemas the API exposes.

Storage uploads hit the same error because files are rows in storage.objects. Supabase’s Storage docs say no uploads are allowed to a bucket without RLS policies, that an upload needs only an INSERT policy, and that overwriting with upsert also needs SELECT and UPDATE. Their per-user folder example:

create policy "users upload to own folder"
on storage.objects
for insert
to authenticated
with check (
  bucket_id = 'my_bucket_id'
  and (storage.foldername(name))[1] = (select auth.jwt()->>'sub')
);

Testing policies as different users

The SQL editor runs as a privileged role, so a test there proves nothing until you switch role. Run the quick-fix block above (set role authenticated) for a basic check, then the pgTAP test in the fixes above, which sets one user’s sub claim and asserts that writing a row owned by a second user fails with 42501. Add a case where the same user inserts their own row and succeeds. Test the logged-out case too, as anon, because auth.uid() is null there. In the browser, sign in as two real test accounts and repeat the insert from each; the Network tab should show a 201 for the owner and an error for the other.

The dangerous fix: disabling RLS or using the service role key

Warning: do not ship the service role key to the browser.

Two fixes make this error disappear and both remove your protection. Disabling RLS lets anyone with your public key, which is visible in your frontend bundle, read and write the table. Putting the service_roleor secret key in client code does the same with full admin rights: Supabase’s docs say to use a secret key only for administrative tasks that need to bypass RLS and never in the browser, and its Storage docs say service keys bypass RLS entirely. If the key has already shipped in a build, rotate it.

The service role does have a correct place. A server-side Edge Function or API route can use it to write rows the browser should never insert directly, such as contact form submissions. In that design a security scanner may flag “anon cannot insert” as broken RLS, but that is the intended result: the browser calls the function, the function validates and inserts. Confirm that no browser code inserts into the table, and that the key lives only in the function’s secrets, before you dismiss the flag.

Fixing it inside Lovable and Bolt apps

In a Lovable or Bolt app the policies live in the Supabase database behind the app, not in the editor’s preview. Open the table’s policy list (the Supabase dashboard, or the builder’s own database view if it manages the project for you), find the cause in the table above, and apply the SQL as a migration so the fix is in the repo rather than only in the dashboard. If you ask the builder’s AI to fix it, read the policy it writes: a with check (true) or a disabled RLS toggle clears the error by removing the protection. Then re-run the two-user test. Our guide to Lovable security issues covers the other holes these apps tend to ship with, and the Supabase auth and database fixes hub collects the related errors.

When to get a security audit

One table with one ownership column is a job you can finish with this page. Get a second pair of eyes when the schema has many tables, organization or team membership, storage buckets with private files, or any place the service role key might have reached the client. Those are the cases where a policy that “works” can still leak rows to another user. For background on how vibe coding security risks show up in production, start there. Not sure your RLS is safe? Send your repo for the $49 async repo audit, or compare code audit services for a deeper review.

Supabase and Postgres documentation checked 4 November 2026.

Tried the fix and still broken?

Send your app URL. A senior engineer records a 10-minute teardown.

Free, and fully async — no call to book. A senior engineer walks your app on video: what’s broken, what’s risky, what to fix first. In your inbox within 24 hours. Want the code read line by line instead? Send the repo for the $49 audit.

Why do AI-built apps hit Supabase RLS blocking insert?

Lovable, Bolt, Base44, and Cursor all ship apps that talk to Supabase, usually straight from the browser with the public (anon or publishable) key. Every one of those requests runs as the anon or authenticated role, and both are evaluated against RLS. Server code holding the service_role or secret key skips RLS when the request carries no user token, so the same insert can work in a script or Edge Function and fail in the app.

Postgres default when RLS is enabled and no policy matches is deny. Your INSERT returns SQLSTATE 42501 with the message “new row violates row-level security policy for table”. If the INSERT is chained to .select(), the returned row must also pass a SELECT policy; the Postgres docs say such rows are never silently dropped, so a missing SELECT policy raises the same error even when the INSERT policy is correct. A second quiet cause: when no user is signed in, auth.uid() returns null, and null = user_id is never true.

The third variant is a policy that exists but checks the wrong thing. A FOR INSERT policy can only carry WITH CHECK; a FOR ALL policy with only a USING clause has that expression reused as the insert check, and WITH CHECK (true) lets any signed-in user write rows for anyone. The policy looks written and the table looks protected, but inserts fail, or succeed for rows they should not. Working through the row-by-row RLS audit checklist catches this class, because it makes you read USING and WITH CHECK separately on every policy instead of accepting that a policy exists at all.

One bypass sits outside the policy set entirely: the service_rolekey ignores RLS by design, so if that key leaks the table is readable no matter what you wrote. Vercel’s April 2026 incident exposed non-sensitive environment variables — which is exactly where most AI-built apps keep that key — so it is worth knowing when the leak is not in your policies. To rewrite the policies for several tables at once, paste your schema into the free Supabase RLS policy generator, which also writes pgTAP tests that expect 42501 on another user’s rows. And once a schema has more tables than you can realistically read policy-by-policy, you can have an engineer read the whole policy set against the schema in one pass rather than table by table.

Diagnose Supabase RLS blocking insert by failure mode

Four causes cover almost every version of this error. Read the policy list for the table in the dashboard, then match what you see to a row.

CauseHow to spot itFix
No INSERT policyRLS is on and the table's policy list has nothing for INSERT (or ALL)create policy … for insert to authenticated with check (auth.uid() = user_id)
auth.uid() mismatch or nullThe request carries only the anon key with no signed-in session, or the client sends a user_id that is not the caller'sSign the user in before the insert; set the column default to auth.uid() and stop sending user_id from the client
Missing or wrong WITH CHECKA FOR ALL policy with only USING, or a check on a column the insert does not setWrite an explicit FOR INSERT policy whose WITH CHECK tests the ownership column
Anon role, or a SELECT policy missing behind .select() or upsertPolicy is scoped to authenticated but the user is logged out; or .insert() works and .insert().select() failsScope the policy to the role that actually inserts; add SELECT (and UPDATE for upsert) policies

Which AI builders ship Supabase RLS blocking insert?

How often each AI builder ships this error and the pattern that produces it.

AI builder × Supabase RLS blocking insert
BuilderFrequencyPattern
LovableEvery Supabase scaffoldRLS enabled with no policy for one of the operations the app uses
Bolt.newCommonFOR ALL policy with USING only, reused as the insert check
Base44CommonFOR ALL WITH CHECK (true) — identical to no protection
CursorSometimesLeaves user_id nullable with no default auth.uid()
Replit AgentRareTargets policies at `public` role — includes anon

How do I stop Supabase RLS blocking insert recurring?

Still stuck with Supabase RLS blocking insert?

Emergency triage · $299 · 48h turnaround
We restore service and write the root-cause report.
start the triage →

Supabase RLS blocking insert questions

What does Postgres error 42501 mean on an INSERT?+
42501 is the Postgres insufficient_privilege SQLSTATE, and Supabase shows it with the message "new row violates row-level security policy for table". It fires when Row Level Security is enabled on the target table but no INSERT policy exists, or the policy's WITH CHECK evaluates to false for the row you are trying to write. The anon or authenticated role cannot write the row. Fix by writing a policy whose WITH CHECK matches the ownership column.
Why does my INSERT return PGRST116 instead of 42501?+
PGRST116 is a PostgREST error meaning the request asked for exactly one row with .single() and got zero (or more than one). On a plain read, a missing SELECT policy hides the row, so zero rows come back. An INSERT chained to .select() behaves differently: Postgres requires returned rows to pass the SELECT policies and raises an error instead of hiding them, so a missing SELECT policy shows up as the same row-level security error. Check the Postgres logs to see which statement failed.
Does enabling RLS without policies block all inserts?+
Yes. Postgres default behavior is deny. The moment you enable RLS on a table with no policy, every operation from every non-superuser role is blocked. Supabase anon and authenticated roles are not superusers. service_role bypasses RLS, which is why inserts work from the server but fail from the browser. Add a policy for every operation you need: SELECT, INSERT, UPDATE, DELETE.
What is the difference between USING and WITH CHECK in an INSERT policy?+
USING filters which existing rows are visible or modifiable. WITH CHECK validates rows being written. An INSERT policy cannot have a USING clause at all, because there is no existing row to filter. For UPDATE both apply. The trap is a FOR ALL policy with only USING: Postgres then uses that USING expression as the check for new rows.
How do I write an insert policy in Supabase?+
Run create policy "users insert own rows" on public.tasks for insert to authenticated with check (auth.uid() = user_id); in the SQL editor, swapping in your table and ownership column. Add a matching SELECT policy if the client chains .select() after the insert, and set the column default to auth.uid() so the client cannot send someone else's ID.
Is it safe to disable RLS?+
Not on any table in a schema the API exposes. Supabase's docs say that once RLS is enabled, no data is accessible through the API with a publishable key until you create policies. Disabling it reverses that: anyone holding the public key, which ships in your frontend, can read and write the table. Write the missing policy instead.
Should I use the service role key to bypass RLS?+
Only on a server you control, such as an Edge Function or API route, and never in the browser. Supabase's docs say to use a secret key for administrative tasks that need to bypass RLS and never to expose it to customers; the service_role key is the legacy form of that key. If your browser code needs the service role key to insert, the real fix is an INSERT policy.
How long does a full Supabase RLS audit take?+
For under 20 tables and single-tenant ownership, a complete audit including policy writes, pgTAP test suite, and cross-tenant verification takes about 1 hour. Multi-tenant apps with organization membership joins take 2-4 hours. Our Security Audit service is fixed-fee at $499 and includes policy writes plus a CI test suite so policies cannot regress silently.
Next step

Ship the fix. Keep the fix.

Emergency Triage restores service in 48 hours. Break the Fix Loop rebuilds CI so this error cannot ship again.

About the author

Hyder Shah leads Afterbuild Labs, shipping production rescues for apps built in Lovable, Bolt.new, Cursor, Replit, v0, and Base44. our rescue methodology.

Sources