Supabase RLS Policies
Write Row Level Security (RLS) policies for Supabase tables, then prove them with pgTAP tests that act as two different users. A policy is not finished until a test shows that a second user cannot read, change, delete, or create rows that belong to the first user.
When to Use This Skill
- Creating a table in an exposed schema (by default
public)
- Adding or changing a policy, a view, or a
security definer function
- Reviewing a migration or pull request that touches RLS
- Answering whether users or tenants are isolated from each other
Workflow
- List the access rules first. For each table, write down which role (
anon or authenticated) may run which operation (select, insert, update, delete) on which rows. Confirm the list with the user before writing SQL. Each rule gets one test.
- Write the migration. Enable RLS, add one policy per operation, and index every column the policies filter on.
- Write a pgTAP test file for the table under
supabase/tests/, using the template below.
- Run
supabase test db and read every line of output.
- Break the policy on purpose, on a local database only. Copy the test file and add a temporary policy with
using (true) right after begin;, for example create policy "tmp_open" on public.notes for select to authenticated using (true);. Run supabase test db against the local stack from supabase start, confirm at least one test fails, then delete the copy. Do the same with a temporary for update and a for delete policy. The file ends with rollback, so the temporary policy is undone even when the run fails or stops early. Never put a temporary policy in a migration, and never run this against a shared or production database. Do not drop the real policy to do this: with no policy every read is denied, so denial tests still pass and prove nothing.
- Do not change a policy just to make a test pass. Report which assertion failed and which policy it covers, and let the user decide which one is wrong.
Rules for Every Table
Enable RLS and name the role
- Run
alter table ... enable row level security on every table in an exposed schema. Once RLS is on and no policy exists, the API returns no rows.
- Add a
to clause to every policy. Without it the policy applies to PUBLIC, which means every role, including anon.
auth.uid() returns null when the request has no signed in user. to authenticated keeps anonymous requests from reaching the check at all.
One policy per operation, with the owner checked on writes
alter table public.notes enable row level security;
create policy "notes_select_own" on public.notes
for select to authenticated
using ((select auth.uid()) = owner_id);
create policy "notes_insert_own" on public.notes
for insert to authenticated
with check ((select auth.uid()) = owner_id);
create policy "notes_update_own" on public.notes
for update to authenticated
using ((select auth.uid()) = owner_id)
with check ((select auth.uid()) = owner_id);
create policy "notes_delete_own" on public.notes
for delete to authenticated
using ((select auth.uid()) = owner_id);
create index notes_owner_id_idx on public.notes using btree (owner_id);
using checks rows that already exist. with check checks the row being written. Rows that fail using are skipped with no error; a row that fails with check raises an error.
- Bind the owner column in every insert and update check. A check such as
auth.uid() is not null only proves the caller is signed in. Any signed in user can then insert a row with another user's id in the owner column.
- A column default such as
default auth.uid() does not protect the owner column. Postgres uses a default only when the insert does not supply a value, and the client can always supply one.
- An
update needs a matching select policy, or the update will not work as expected.
- Wrap
auth.uid() and auth.jwt() in (select ...) so Postgres runs them once per statement instead of once per row.
- Index every column a policy filters on. A column counts as indexed only when it is the first column of a btree index.
Authorization data in the token
- Never base a policy on
user_metadata (raw_user_meta_data). Signed in users can change it themselves.
- Store roles, teams, and plans in
app_metadata (raw_app_meta_data), which users cannot change, and read it with auth.jwt().
- A change to
app_metadata reaches auth.jwt() only after the user's token is refreshed. A user removed from a team keeps access until then.
Multi factor authentication
To require a second factor, add a restrictive policy:
create policy "notes_require_aal2" on public.notes
as restrictive
for all to authenticated
using ((select auth.jwt()->>'aal') = 'aal2');
Restrictive policies only narrow access. A table with only restrictive policies returns no rows, so keep the permissive policies above.
Views
Views bypass RLS by default because they run with the rights of the role that created them. On Postgres 15 and later, create views with security_invoker = true:
create view public.my_notes
with (security_invoker = true)
as select id, body from public.notes;
On older versions, remove every grant path or keep the view in a schema that is not exposed. Revoking only the direct grants is not enough: a grant to PUBLIC, a grant to a role the API roles are members of, or a grant on a single column still lets them read the view. Check the result with has_any_column_privilege, which counts all three; relacl lists only direct table grants.
revoke all on public.my_notes from public, anon, authenticated;
select has_any_column_privilege('anon', 'public.my_notes', 'select'),
has_any_column_privilege('authenticated', 'public.my_notes', 'select'); -- both must be false
Security definer functions
- A
security definer function runs as its owner. If the owner is postgres, it bypasses RLS.
- Never create one in an exposed schema. It can be called over the Data API with the owner's rights.
- Set
search_path = '' and schema qualify every name inside the function.
Keys
- Secret keys (and the legacy
service_role key) bypass RLS. Use them only on a server, never in a browser or mobile app.
- A secret key bypasses RLS only when the request has no user access token. If the request carries one, that user's policies apply.
Two User pgTAP Test Template
Save as supabase/tests/notes_rls_test.sql and adapt the table, columns, and ids. The file runs in one transaction and rolls back, so test data never persists.
begin;
create extension if not exists pgtap with schema extensions;
select plan(8);
-- Two users, created before switching roles.
insert into auth.users (id, email) values
('11111111-1111-1111-1111-111111111111', 'user1@test.com'),
('22222222-2222-2222-2222-222222222222', 'user2@test.com');
insert into public.notes (id, owner_id, body) values
('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '11111111-1111-1111-1111-111111111111', 'user 1 note');
-- As user 1: the owner can read and change their own row.
set local role authenticated;
set local request.jwt.claims = '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated"}';
select results_eq(
$$select body from public.notes where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'$$,
$$values ('user 1 note'::text)$$,
'user 1 reads their own note'
);
select results_eq(
$$update public.notes set body = 'edited' where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' returning body$$,
$$values ('edited'::text)$$,
'user 1 updates their own note'
);
-- As user 2: every attempt on user 1's row must fail.
-- User 2 owns no rows yet, so the write probes need no where clause.
set local request.jwt.claims = '{"sub": "22222222-2222-2222-2222-222222222222", "role": "authenticated"}';
select is_empty(
$$select 1 from public.notes where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'$$,
'user 2 cannot read user 1 note'
);
select is_empty(
$$update public.notes set body = 'changed' returning 1$$,
'user 2 cannot update user 1 note'
);
select is_empty(
$$delete from public.notes returning 1$$,
'user 2 cannot delete user 1 note'
);
select throws_ok(
$$insert into public.notes (owner_id, body) values ('11111111-1111-1111-1111-111111111111', 'forged')$$,
'42501', null,
'user 2 cannot create a note owned by user 1'
);
select results_eq(
$$insert into public.notes (owner_id, body) values ('22222222-2222-2222-2222-222222222222', 'user 2 note') returning owner_id$$,
$$values ('22222222-2222-2222-2222-222222222222'::uuid)$$,
'user 2 creates their own note'
);
-- No where clause: the update policy alone picks user 2's row,
-- so only its with check can stop the transfer.
select throws_ok(
$$update public.notes set owner_id = '11111111-1111-1111-1111-111111111111'$$,
'42501', null,
'user 2 cannot hand their note to user 1'
);
select * from finish();
rollback;
Notes on the template:
Use returning on writes. An update or delete that RLS filters out succeeds with zero rows, so only the returned rows show what happened.
In user 2's update and delete probes, do not filter on a column or return one. When an update or delete reads a column, Postgres applies the select policy as well, so a correct select policy can hide an update or delete policy that is too open. Return the constant 1 and rely on user 2 owning no rows yet.
Filter reads by the ids the test inserted. A count over the whole table breaks once other data exists.
Error 42501 is the code Postgres raises when a written row fails a with check expression.
Add an anon case for any table that anonymous visitors must not read, and add one to the plan count. Replace the claims first: changing the role does not clear them, so auth.uid() would still return user 2's id.
set local request.jwt.claims = '{"role": "anon"}';
set local role anon;
select is_empty(
$$select 1 from public.notes where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'$$,
'an anonymous visitor cannot read user 1 note'
);
If your project does not grant table privileges to anon, the read raises 42501 instead, so use throws_ok.
Testing Policies That Read Other Claims
The template sets the whole claims object in request.jwt.claims, the setting PostgREST fills from the token. auth.uid() and auth.jwt() both read it. If a test sets only request.jwt.claim.sub, auth.uid() still works but auth.jwt() returns null, so a policy that compares a claim such as aal or a value in app_metadata sees null. The aal2 policy above denies every user in that state, so a test that expects a denial passes for the wrong reason.
For a table with the restrictive aal2 policy, add "aal": "aal2" to the claims at both user switches in the template, so the owner checks still pass. Then test the other side before select * from finish();, and add these two assertions to the plan count:
-- If the anon case ran first, the SQL role is still anon. The role claim in the JSON does not change it.
set local role authenticated;
set local request.jwt.claims = '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated", "aal": "aal1"}';
select is_empty(
$$select 1 from public.notes where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'$$,
'an aal1 session cannot read user 1 note'
);
set local request.jwt.claims = '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated", "aal": "aal2"}';
select isnt_empty(
$$select 1 from public.notes where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'$$,
'an aal2 session reads user 1 note'
);
Review Checklist
References
1---2name: supabase-rls-policies3description: 'Write, review, and test Supabase Row Level Security (RLS) policies so that one user cannot read or change another user''s rows. Use when creating or changing tables, policies, views, or security definer functions in a Supabase project, when reviewing a migration that touches RLS, or when asked to prove user isolation with pgTAP tests that sign in as two users.'4metadata:5 source: https://skillmd.com/skills/github/supabase-rls-policies6---78# Supabase RLS Policies910Write Row Level Security (RLS) policies for Supabase tables, then prove them with pgTAP tests that act as two different users. A policy is not finished until a test shows that a second user cannot read, change, delete, or create rows that belong to the first user.1112## When to Use This Skill1314- Creating a table in an exposed schema (by default `public`)15- Adding or changing a policy, a view, or a `security definer` function16- Reviewing a migration or pull request that touches RLS17- Answering whether users or tenants are isolated from each other1819## Workflow20211. **List the access rules first.** For each table, write down which role (`anon` or `authenticated`) may run which operation (`select`, `insert`, `update`, `delete`) on which rows. Confirm the list with the user before writing SQL. Each rule gets one test.222. **Write the migration.** Enable RLS, add one policy per operation, and index every column the policies filter on.233. **Write a pgTAP test file** for the table under `supabase/tests/`, using the template below.244. **Run `supabase test db`** and read every line of output.255. **Break the policy on purpose, on a local database only.** Copy the test file and add a temporary policy with `using (true)` right after `begin;`, for example `create policy "tmp_open" on public.notes for select to authenticated using (true);`. Run `supabase test db` against the local stack from `supabase start`, confirm at least one test fails, then delete the copy. Do the same with a temporary `for update` and a `for delete` policy. The file ends with `rollback`, so the temporary policy is undone even when the run fails or stops early. Never put a temporary policy in a migration, and never run this against a shared or production database. Do not drop the real policy to do this: with no policy every read is denied, so denial tests still pass and prove nothing.266. **Do not change a policy just to make a test pass.** Report which assertion failed and which policy it covers, and let the user decide which one is wrong.2728## Rules for Every Table2930### Enable RLS and name the role3132- Run `alter table ... enable row level security` on every table in an exposed schema. Once RLS is on and no policy exists, the API returns no rows.33- Add a `to` clause to every policy. Without it the policy applies to `PUBLIC`, which means every role, including `anon`.34- `auth.uid()` returns `null` when the request has no signed in user. `to authenticated` keeps anonymous requests from reaching the check at all.3536### One policy per operation, with the owner checked on writes3738```sql39alter table public.notes enable row level security;4041create policy "notes_select_own" on public.notes42 for select to authenticated43 using ((select auth.uid()) = owner_id);4445create policy "notes_insert_own" on public.notes46 for insert to authenticated47 with check ((select auth.uid()) = owner_id);4849create policy "notes_update_own" on public.notes50 for update to authenticated51 using ((select auth.uid()) = owner_id)52 with check ((select auth.uid()) = owner_id);5354create policy "notes_delete_own" on public.notes55 for delete to authenticated56 using ((select auth.uid()) = owner_id);5758create index notes_owner_id_idx on public.notes using btree (owner_id);59```6061- `using` checks rows that already exist. `with check` checks the row being written. Rows that fail `using` are skipped with no error; a row that fails `with check` raises an error.62- **Bind the owner column in every insert and update check.** A check such as `auth.uid() is not null` only proves the caller is signed in. Any signed in user can then insert a row with another user's id in the owner column.63- A column default such as `default auth.uid()` does not protect the owner column. Postgres uses a default only when the insert does not supply a value, and the client can always supply one.64- An `update` needs a matching `select` policy, or the update will not work as expected.65- Wrap `auth.uid()` and `auth.jwt()` in `(select ...)` so Postgres runs them once per statement instead of once per row.66- Index every column a policy filters on. A column counts as indexed only when it is the first column of a btree index.6768### Authorization data in the token6970- Never base a policy on `user_metadata` (`raw_user_meta_data`). Signed in users can change it themselves.71- Store roles, teams, and plans in `app_metadata` (`raw_app_meta_data`), which users cannot change, and read it with `auth.jwt()`.72- A change to `app_metadata` reaches `auth.jwt()` only after the user's token is refreshed. A user removed from a team keeps access until then.7374### Multi factor authentication7576To require a second factor, add a restrictive policy:7778```sql79create policy "notes_require_aal2" on public.notes80 as restrictive81 for all to authenticated82 using ((select auth.jwt()->>'aal') = 'aal2');83```8485Restrictive policies only narrow access. A table with only restrictive policies returns no rows, so keep the permissive policies above.8687### Views8889Views bypass RLS by default because they run with the rights of the role that created them. On Postgres 15 and later, create views with `security_invoker = true`:9091```sql92create view public.my_notes93 with (security_invoker = true)94 as select id, body from public.notes;95```9697On older versions, remove every grant path or keep the view in a schema that is not exposed. Revoking only the direct grants is not enough: a grant to `PUBLIC`, a grant to a role the API roles are members of, or a grant on a single column still lets them read the view. Check the result with `has_any_column_privilege`, which counts all three; `relacl` lists only direct table grants.9899```sql100revoke all on public.my_notes from public, anon, authenticated;101select has_any_column_privilege('anon', 'public.my_notes', 'select'),102 has_any_column_privilege('authenticated', 'public.my_notes', 'select'); -- both must be false103```104105### Security definer functions106107- A `security definer` function runs as its owner. If the owner is `postgres`, it bypasses RLS.108- Never create one in an exposed schema. It can be called over the Data API with the owner's rights.109- Set `search_path = ''` and schema qualify every name inside the function.110111### Keys112113- Secret keys (and the legacy `service_role` key) bypass RLS. Use them only on a server, never in a browser or mobile app.114- A secret key bypasses RLS only when the request has no user access token. If the request carries one, that user's policies apply.115116## Two User pgTAP Test Template117118Save as `supabase/tests/notes_rls_test.sql` and adapt the table, columns, and ids. The file runs in one transaction and rolls back, so test data never persists.119120```sql121begin;122create extension if not exists pgtap with schema extensions;123select plan(8);124125-- Two users, created before switching roles.126insert into auth.users (id, email) values127 ('11111111-1111-1111-1111-111111111111', 'user1@test.com'),128 ('22222222-2222-2222-2222-222222222222', 'user2@test.com');129130insert into public.notes (id, owner_id, body) values131 ('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '11111111-1111-1111-1111-111111111111', 'user 1 note');132133-- As user 1: the owner can read and change their own row.134set local role authenticated;135set local request.jwt.claims = '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated"}';136137select results_eq(138 $$select body from public.notes where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'$$,139 $$values ('user 1 note'::text)$$,140 'user 1 reads their own note'141);142143select results_eq(144 $$update public.notes set body = 'edited' where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' returning body$$,145 $$values ('edited'::text)$$,146 'user 1 updates their own note'147);148149-- As user 2: every attempt on user 1's row must fail.150-- User 2 owns no rows yet, so the write probes need no where clause.151set local request.jwt.claims = '{"sub": "22222222-2222-2222-2222-222222222222", "role": "authenticated"}';152153select is_empty(154 $$select 1 from public.notes where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'$$,155 'user 2 cannot read user 1 note'156);157158select is_empty(159 $$update public.notes set body = 'changed' returning 1$$,160 'user 2 cannot update user 1 note'161);162163select is_empty(164 $$delete from public.notes returning 1$$,165 'user 2 cannot delete user 1 note'166);167168select throws_ok(169 $$insert into public.notes (owner_id, body) values ('11111111-1111-1111-1111-111111111111', 'forged')$$,170 '42501', null,171 'user 2 cannot create a note owned by user 1'172);173174select results_eq(175 $$insert into public.notes (owner_id, body) values ('22222222-2222-2222-2222-222222222222', 'user 2 note') returning owner_id$$,176 $$values ('22222222-2222-2222-2222-222222222222'::uuid)$$,177 'user 2 creates their own note'178);179180-- No where clause: the update policy alone picks user 2's row,181-- so only its with check can stop the transfer.182select throws_ok(183 $$update public.notes set owner_id = '11111111-1111-1111-1111-111111111111'$$,184 '42501', null,185 'user 2 cannot hand their note to user 1'186);187188select * from finish();189rollback;190```191192Notes on the template:193194- Use `returning` on writes. An update or delete that RLS filters out succeeds with zero rows, so only the returned rows show what happened.195- In user 2's update and delete probes, do not filter on a column or return one. When an update or delete reads a column, Postgres applies the `select` policy as well, so a correct `select` policy can hide an `update` or `delete` policy that is too open. Return the constant `1` and rely on user 2 owning no rows yet.196- Filter reads by the ids the test inserted. A count over the whole table breaks once other data exists.197- Error `42501` is the code Postgres raises when a written row fails a `with check` expression.198- Add an `anon` case for any table that anonymous visitors must not read, and add one to the plan count. Replace the claims first: changing the role does not clear them, so `auth.uid()` would still return user 2's id.199200 ```sql201 set local request.jwt.claims = '{"role": "anon"}';202 set local role anon;203 select is_empty(204 $$select 1 from public.notes where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'$$,205 'an anonymous visitor cannot read user 1 note'206 );207 ```208209 If your project does not grant table privileges to `anon`, the read raises `42501` instead, so use `throws_ok`.210211## Testing Policies That Read Other Claims212213The template sets the whole claims object in `request.jwt.claims`, the setting PostgREST fills from the token. `auth.uid()` and `auth.jwt()` both read it. If a test sets only `request.jwt.claim.sub`, `auth.uid()` still works but `auth.jwt()` returns `null`, so a policy that compares a claim such as `aal` or a value in `app_metadata` sees `null`. The `aal2` policy above denies every user in that state, so a test that expects a denial passes for the wrong reason.214215For a table with the restrictive `aal2` policy, add `"aal": "aal2"` to the claims at both user switches in the template, so the owner checks still pass. Then test the other side before `select * from finish();`, and add these two assertions to the plan count:216217```sql218-- If the anon case ran first, the SQL role is still anon. The role claim in the JSON does not change it.219set local role authenticated;220set local request.jwt.claims = '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated", "aal": "aal1"}';221select is_empty(222 $$select 1 from public.notes where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'$$,223 'an aal1 session cannot read user 1 note'224);225226set local request.jwt.claims = '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated", "aal": "aal2"}';227select isnt_empty(228 $$select 1 from public.notes where id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'$$,229 'an aal2 session reads user 1 note'230);231```232233## Review Checklist234235- [ ] RLS is enabled on every table in an exposed schema236- [ ] Every policy names a role with `to`237- [ ] Insert and update checks bind the owner column to `(select auth.uid())`238- [ ] No policy reads `user_metadata`239- [ ] Views use `security_invoker = true`, or are not exposed240- [ ] Security definer functions live outside exposed schemas and set `search_path = ''`241- [ ] Each policy column has an index that leads with that column242- [ ] Each user data table has a two user test covering read, update, delete, and insert243- [ ] The second user's update and delete probes read no column244- [ ] Tests set the full `request.jwt.claims` object at each user switch, and replace it before the `anon` case245- [ ] The suite fails when a temporary `using (true)` policy is added inside the test transaction on a local database246247## References248249- [Row Level Security](https://supabase.com/docs/guides/database/postgres/row-level-security), Supabase docs250- [Test Row Level Security policies with pgTAP](https://supabase.com/docs/guides/database/postgres/testing-row-level-security), Supabase docs251- [Multi-Factor Authentication](https://supabase.com/docs/guides/auth/auth-mfa), Supabase docs252- [CREATE POLICY](https://www.postgresql.org/docs/current/sql-createpolicy.html), PostgreSQL docs253- [Transactions](https://docs.postgrest.org/en/stable/references/transactions.html), PostgREST docs (`request.jwt.claims`)254- [CREATE TABLE](https://www.postgresql.org/docs/current/sql-createtable.html), PostgreSQL docs (column defaults)255- [auth.uid() source](https://github.com/supabase/auth/blob/35307ce6c1ca59016fd820f1d77d9b6201dc2e15/migrations/20220224000811_update_auth_functions.up.sql) and [auth.jwt() source](https://github.com/supabase/auth/blob/35307ce6c1ca59016fd820f1d77d9b6201dc2e15/migrations/20220531120530_add_auth_jwt_function.up.sql), Supabase Auth