Supabase "new row violates row-level security policy" is a check problem, not a credential problem
“New row violates row-level security policy” is the error you get when a write — or the read-back of that write — fails a check. Three different checks produce it, and one of them has nothing to do with the policy you are staring at. I measured twenty-six INSERT-path scenarios on a local PostgreSQL 17.12 container with roles shaped like Supabase's anon and service_role. Four of them look identical in the client and are fixed in completely different places.
The write check itself
The plain cases first. With RLS enabled, an INSERT must pass a WITH CHECK expression on a policy that covers both the INSERT command and your role. These are the modes most people eventually find:
| # | Setup | Measured signature | What it proves |
|---|---|---|---|
| 1 | RLS on, no policy at all, INSERT as authenticated | new row violates row-level security policy for table "s1" | With RLS on and no policy, the write side is default-deny. Nothing to grant, nothing to fix in code. |
| 2 | RLS on, SELECT-only policy USING (true), INSERT | new row violates ... for table "s2" | A SELECT policy never authorises a write. USING (true) reads everything and writes nothing. |
| 3 | Policy FOR INSERT TO anon, INSERT as authenticated | new row violates ... for table "s3" | The policy exists but its role list does not include you — same error as having no policy. |
| 4 | Policy FOR ALL USING (true), WITH CHECK omitted, INSERT | INSERT 0 1 — row readable afterwards | FOR ALL covers writes too, and its USING doubles as WITH CHECK. This is the only shape that reads and writes with one clause. |
| 5 | Policy FOR INSERT WITH CHECK (tenant = jwt sub), own tenant | INSERT 0 1 | The write check passed. This is the healthy baseline. |
| 6 | Same policy, foreign tenant | new row violates ... for table "s5" | WITH CHECK is evaluated against the row you are writing, not the rows you can see. |
| 7 | Policy WITH CHECK (true), no INSERT grant | permission denied for table s7 | Different signature entirely: the grant gate, not the policy gate. Policies cannot grant. |
| 8 | CREATE POLICY ... FOR INSERT USING (...) instead of WITH CHECK | ERROR: only WITH CHECK expression allowed for INSERT | The DDL refuses before any data moves. USING belongs to read and update paths only. |
| 9 | INSERT-only policy, INSERT grant only, plain INSERT | INSERT 0 1 | The trap's innocent half: the insert that actually works. Everything below fails on the same table. |
| 10 | Same table, INSERT ... RETURNING id, no SELECT grant | permission denied for table q2 | RETURNING reads the row back, and reading needs SELECT privilege. This is the plain 42501 behind many "Supabase insert 403" reports. |
| 11 | SELECT granted, still no SELECT policy, INSERT ... RETURNING id | new row violates ... for table "q3" | Privilege fixed, policy still missing: the read-back now fails the RLS gate with the very error people chase in the policy they just wrote. |
| 12 | SELECT granted, SELECT policy USING (false), INSERT ... RETURNING id | new row violates ... for table "r5" | The read-back is really gated by the SELECT policy. USING (false) turns a passing write into this error. |
| 13 | SELECT granted, SELECT policy USING (true), INSERT ... RETURNING id | INSERT 0 1 — row returned | Three gates passed. RETURNING works only when all of grant + write policy + read policy pass. |
| 14 | INSERT-only policy, plain INSERT, then SELECT as the same role | INSERT 0 1 — then 0 rows on read-back | The silent twin: the write succeeds and the row is invisible. No error anywhere. |
Mode 8 is worth pinning up: an INSERT policy takes WITH CHECK, not USING. PostgreSQL rejects USING on an insert policy with only WITH CHECK expression allowed for INSERT — before any row moves. If you were about to debug that DDL at runtime, stop: it never ran.
The part that isn't your policy at all: RETURNING
Modes 9–11 are one table, one policy, one grant set. Plain INSERT works. Add returning id and it fails — and the error is the same one you were already chasing.
- No SELECT grant →
permission denied for table q2(mode 10). RETURNING reads the row back; reading needs SELECT privilege. - SELECT granted, no SELECT policy →
new row violates row-level security policy(mode 11). - SELECT policy that the row fails → same error, from a policy that looks completely unrelated to your insert (mode 12).
- SELECT policy the row passes → the row comes back (mode 13).
So INSERT ... RETURNING under RLS is really an insert plus a read, and both gates must pass. This matters because return=representation is the default shape of most clients: PostgREST applies it when the request asks for it, and supabase-js .insert() without .select() still resolves the written row back in several call paths. The fix is not to touch the insert policy — it is to add a SELECT policy the new row satisfies.
Mode 14 is the quiet version: no RETURNING, no error, row stored, then a plain SELECT returns 0rows. If your dashboard shows “nothing saved” and no error, check for a SELECT policy before you check anything else.
Upserts need two more policies
An INSERT ... ON CONFLICT reads the table to detect the conflict before it writes. That read path needs a SELECT grant and a SELECT policy; the update branch needs an UPDATE policy as well.
| # | Setup | Measured signature | What it proves |
|---|---|---|---|
| 15 | ON CONFLICT (id) DO UPDATE, insert-only policy, empty table | new row violates ... for table "r7" | No conflicting row exists, so nothing was updated — and it still fails. The conflict check itself needs a read path. |
| 16 | ON CONFLICT (id) DO NOTHING, insert-only policy, empty table | new row violates ... for table "r8" | DO NOTHING is not a way around the read path either. |
| 17 | ON CONFLICT (id) DO NOTHING, insert grant but no SELECT grant | permission denied for table r16 | The upsert needs SELECT privilege before policies matter. supabase-js .upsert() hits this first. |
| 18 | DO NOTHING + insert policy + select policy, second insert conflicts | INSERT 0 0 — RETURNING returns 0 rows | It works now, and the conflict is silent: the row is still there, nothing was written, no error. This is the quiet data-loss shape. |
| 19 | DO UPDATE + insert policy + select policy, no update policy | new row violates row-level security policy (USING expression) for table "r14" | A third message variant. The (USING expression) tag means the failing check was a read/update USING clause, not WITH CHECK. |
| 20 | DO UPDATE + insert policy + update policy, no select policy | new row violates ... for table "r15" | The other half of the pair: an upsert needs BOTH a select policy and an update policy. |
| 21 | DO UPDATE + insert + select + update policies | UPDATE applied — note column changed | All four policies present. The upsert finally does what it says. |
Read mode 19 twice. new row violates row-level security policy (USING expression) is a different message from the plain variant, and the parenthetical is the whole diagnosis: the check that failed was a USING clause on a read/update path, not a WITH CHECK on your insert. If you see the tag, stop editing insert policies.
Sequences, owners, and bypass roles
| # | Setup | Measured signature | What it proves |
|---|---|---|---|
| 22 | serial column, insert-only policy, no USAGE on the sequence | permission denied for sequence r10_id_seq | The table grant carried, the sequence grant did not. Sequences have their own ACL. |
| 23 | Same serial column, id supplied explicitly | INSERT 0 1 | Supplying the id skips the sequence entirely — a quick bisect for sequence-vs-policy questions. |
| 24 | Table owner INSERTs with only an INSERT policy TO authenticated | INSERT 0 1 | Owners bypass RLS by default. The policy never ran for this statement. |
| 25 | Same owner after ALTER TABLE ... FORCE ROW LEVEL SECURITY | new row violates ... for table "s8b" | FORCE RLS closes the owner bypass — and the owner now gets the same error as the app role. |
| 26 | service_role (rolbypassrls = t, measured) INSERTs under a deny policy | INSERT 0 1 — then permission denied on the follow-up SELECT | BYPASSRLS skips policies, never ACLs. The insert lands; a SELECT without a grant still 42501s. |
Modes 24 and 25 are the pair that makes “it works in the SQL editor” a worthless test. The editor runs as a superuser-like owner, relforcerowsecurity is off by default, and the policy set never applies. Mode 25 shows the same statement failing for the owner the moment FORCE ROW LEVEL SECURITY is on.
The 30-second diagnostic
Run these read-only as the project role. Each one is verified against a fixture table (orders) with deliberately asymmetric policies — insert-only, select granted, update not:
| Query | What it tells you | Measured result |
|---|---|---|
| select polname, polcmd::text as cmd, case when 0 = any(polroles) then 'PUBLIC' else (select string_agg(r.rolname,',') from pg_roles r where r.oid = any(polroles)) end as applies_to, polpermissive::text as permissive, coalesce(pg_get_expr(polqual, polrelid),'-') as using_expr, coalesce(pg_get_expr(polwithcheck, polrelid),'-') as with_check_expr from pg_policy p where polrelid = 'public.orders'::regclass order by polname; | Every policy on the table, parsed | one row: orders_ins | a | authenticated | true | - | true — cmd a = INSERT only, WITH CHECK true, no USING |
| select c.cmd, count(p.polname) as policies from (values ('r'),('a'),('w'),('d'),('*')) as c(cmd) left join pg_policy p on p.polcmd::text = c.cmd and p.polrelid = 'public.orders'::regclass group by c.cmd order by c.cmd; | Which commands have any policy at all | r: 0, a: 1, w: 0, d: 0, *: 0 — the zeros are the RETURNING and upsert gaps |
| select has_table_privilege('authenticated','public.orders','INSERT') as can_insert, has_table_privilege('authenticated','public.orders','SELECT') as can_select, has_table_privilege('authenticated','public.orders','UPDATE') as can_update, has_sequence_privilege('authenticated','public.orders_id_seq','USAGE') as can_use_seq; | The grant gate, per command | t | t | f | t on my fixture — insert and select open, update closed, sequence usable |
| select grantee, privilege_type from information_schema.role_table_grants where table_schema='public' and table_name='orders' order by grantee, privilege_type; | Who actually holds what | authenticated: INSERT, SELECT — and nothing else |
| select relrowsecurity, relforcerowsecurity from pg_class where relname = 'orders'; | Whether policies apply, and to the owner too | t | f — policies on for app roles, still off for the owner |
The second query is the one that finds the RETURNING and upsert failures in a single look: r: 0 and w: 0 against an a: 1 is the exact shape of a table where plain inserts work, .select() chains break, and .upsert() throws.
The fix, in gate order
- Separate the gates before editing policies.
permission denied for tableandpermission denied for sequenceare grant problems;new row violatesis a policy problem; the(USING expression)tag is a read/update-policy problem. Three fixes, three places. - Match the policy set to the commands you actually run. If the client reads the row back, the table needs a SELECT policy; if it upserts, it needs SELECT and UPDATE policies beside the INSERT one. Four statements, four command types.
- Check the role list. Empty
polrolesmeans PUBLIC; a named list excludes everyone else (mode 3).TO authenticatedon a policy that anon calls is indistinguishable from no policy. - Grant sequences separately —
grant usage, select on all sequences in schema public to anon, authenticated;— and remember thatalter default privilegesis what keeps the next table from restarting at mode 7. - Test as the app role.
set local role authenticated;withrequest.jwt.claimsset reproduces the app path exactly; the SQL editor will not (modes 24–25).
Honest limits of these measurements
- Local PostgreSQL 17.12 container, plain login roles shaped like
anon/authenticated/service_role. Hosted Supabase provisions those roles with its own defaults; I did not run this on a hosted project. - Client behaviour (
return=representation, supabase-js.select()chains) is described from the client documentation and the Postgres behaviour behind it — I measured the database side, not each SDK version. - Message text is the raw server message. PostgREST wraps these into
403/400bodies with its own fields, so the(USING expression)tag may be the only discriminator you see.
What this has to do with RLS reviews
Modes 1–13 are the missing-policy direction. The free drift monitor I publish checks the opposite direction — policies that exist but do not protect: owner bypass without FORCE, USING (true) on write commands, missing WITH CHECK on insert policies, SECURITY DEFINER functions without a pinned search_path. A table that passes every check in this article can still be wide open, and a table that fails one of them can still be perfectly safe to read. Both directions, nightly, against your own baseline.
The free drift monitor checks thirteen read-only catalogue conditions nightly and alerts only on changes from your own baseline. The $95 spot check is when you want a second pair of eyes on exactly these signatures before you touch production.
Prefer the verdict instead of running the toolkit yourself?
Fixed-price reviews with the evidence, the SQL and the written verdict. Fixes are quoted separately. Instant checkout, no account needed.
Want the free first step? The RLS Drift Monitor runs 13 read-only catalog checks on a schedule. It never reads application rows; zero findings is not proof of safety.
Prefer escrow? $49 drift monitor setup or $95 exposed-key triage on Upwork. Work starts only after the fixed-price milestone is funded.
Work starts only after payment is secured. Catalogue checks are read-only; no write credentials, no service_role key, no application rows.