Test Your Supabase RLS Policies Before Your Users Do

You test Supabase RLS policies with pgTAP: a suite the Supabase CLI runs with supabase test db that goes red the moment a stranger can read or write your tables, so a policy mistake fails a test instead of shipping. This post is the suite, its real output, and the setup. Row level security explained covers what RLS is and how to write a first policy; this is the proof half.
Three promises are worth testing on every table that holds user data: strangers get nothing, a signed-in user sees only their own rows, and nobody can edit or forge someone else’s rows. Here is the suite’s output on a scratch Postgres with the table left open (RLS off, old default grants), then after the fix (RLS on, owner-only select policy, anon revoked):
before: after:ok 1 - bob's note exists ok 1 - bob's note existsnot ok 2 - anon is refused ok 2 - anon is refusednot ok 3 - alice sees only her own notes ok 3 - alice sees only her own notesok 4 - alice can insert her own note ok 4 - alice can insert her own notenot ok 5 - alice cannot update bob's note ok 5 - alice cannot update bob's notenot ok 6 - alice cannot insert a note... ok 6 - alice cannot insert a note as bobnot ok 7 - bob's notes are unchanged ok 7 - bob's notes are unchangedThe regression test comes first: the failing run is the point. If the suite is green against a table you have not fixed yet, the test is wrong, not the table.
Set it up
The suite is pgTAP, a Postgres testing add-on the Supabase CLI runs with supabase test db. You need the CLI and a local stack (supabase start, which needs Docker or a compatible container manager). Save the file below as supabase/tests/notes_rls.test.sql, or download it from the example folder.
It assumes your notes table has user_id and content columns; rename them to match yours. The begin; ... rollback; wrapper removes the seed rows afterward. If the seed errors on a foreign key from user_id to auth.users, insert the two users into auth.users first, or run it against a scratch table without the key. If you would rather not set any of this up, hand your AI the prompt at the end.
begin;select plan(7);
-- Seed two users' notes (runs as the superuser, so RLS does not apply yet).insert into public.notes (user_id, content) values ('11111111-1111-1111-1111-111111111111', 'alice private note'), ('22222222-2222-2222-2222-222222222222', 'bob private note');
-- Control: Bob's row exists, so the checks below cannot pass by accident.select is( (select count(*)::int from public.notes where user_id = '22222222-2222-2222-2222-222222222222'), 1, 'bob''s note exists');
-- A stranger is refused. With no grant for anon the error is 42501.-- (On an old project that still grants anon, use is_empty instead.)set local role anon;select throws_ok('select content from public.notes', '42501', null, 'anon is refused');
-- Pretend to be Alice: the JWT claims are how Supabase says who is asking.set local role authenticated;set local request.jwt.claims = '{"sub": "11111111-1111-1111-1111-111111111111"}';select results_eq( 'select content from public.notes order by id', $$ values ('alice private note') $$, 'alice sees only her own notes');
-- Control: the policies do not lock the owner out of her own writes.select lives_ok( $$ insert into public.notes (user_id, content) values ('11111111-1111-1111-1111-111111111111', 'alice second note') $$, 'alice can insert her own note');
-- RLS hides rows from update; it does not raise an error. Zero rows changed.select is_empty( $$ update public.notes set content = 'hacked' where user_id = '22222222-2222-2222-2222-222222222222' returning 1 $$, 'alice cannot update bob''s note');
-- Alice cannot write a note as Bob either.select throws_ok( $$ insert into public.notes (user_id, content) values ('22222222-2222-2222-2222-222222222222', 'forged') $$, '42501', null, 'alice cannot insert a note as bob');
-- Back as the superuser: Bob's note is untouched.reset role;select is( (select string_agg(content, ', ' order by id) from public.notes where user_id = '22222222-2222-2222-2222-222222222222'), 'bob private note', 'bob''s notes are unchanged');
select * from finish();rollback;Details the docs skip
- Test 1 is a control. RLS hides rows from
updateanddeleteinstead of raising an error, so “zero rows changed” is also what a typo in Bob’s id looks like. Without the control, test 4 can pass for the wrong reason. - Test 2 expects
42501becauseanonhas no grant. On an old project that still grantsanon, useis_empty: RLS returns nothing instead of refusing. Watch for the 2026 change here: new tables inpublicno longer get default grants, so42501becomes the common case (grants vs RLS explains the two layers). - Test 4 is the second control. A policy that is too tight breaks the app as surely as a loose one exposes it: if the owner cannot insert her own note, launch is broken either way.
- Broken on purpose, here is exactly what catches what. Loosening the select policy to
using (true)turned test 3 red and nothing else: Alice sees Bob’s row, but with no update policy she still cannot change it. Adding an update policy withusing (true)on top turned nothing red, because an update has to see a row to change it. Opening both turned tests 3, 5 and 7 red. All three runs were executed, not reasoned about. - The claims line sets only
sub. That is enough forauth.uid(). A policy that readsauth.jwt()for a role or app metadata needs those claims set too.
The tests check behavior, not implementation
Each assertion describes what a user experiences, not how the policies are written: what Alice can see, what Alice can change, what a stranger gets. That is deliberate. As a check, the select policy was rewritten to call a security definer function and the same six tests passed, because the behavior did not change. Break one of the promises and a test goes red. This is also the honest limit: tests only protect the behavior they cover, so add an assertion for every promise you would hate to lose.
This suite ran on local Postgres 14 with the Supabase roles and auth.uid() emulated, not on a hosted project or through supabase test db itself. The pgTAP calls are the same either way; the CLI path is the one the Supabase docs walk through.
A prompt if you skip the setup
Write a pgTAP test file for my Supabase project. For every table in the public schema, test three behaviors: a stranger gets nothing, a signed-in user sees only their own rows, and a signed-in user cannot update or insert another user’s rows. Include a control test that proves the seed data exists, so the other tests cannot pass by accident. Tell me how to run it.
The policies this suite checks are in row level security explained, and the two-minute pg_tables check there is worth rerunning after every new table the AI creates. If your project predates RLS entirely, the 20-minute security check finds the tables that need it first.
The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.