Multi-tenant Supabase RLS: the org_id membership pattern, measured on PostgreSQL 17
A multi-tenant app stores every row with an org_id and decides access through a membership table. The instinctive policy — "users see rows where org_id matches their org" — is easy to write and easy to get half wrong. B
A multi-tenant app stores every row with an org_id and decides access through a membership table. The instinctive policy — "users see rows where org_id matches their org" — is easy to write and easy to get half wrong.
Below is a real PostgreSQL 17 fixture with four read-only catalog checks and three runtime identity tests, plus the one write-path gap that standard advice misses.
The fixture
Three tables, two orgs, three users, three projects:
create table public.orgs (
id uuid primary key,
name text not null
);
create table public.org_members (
org_id uuid not null references public.orgs(id),
user_id uuid not null,
role text not null default 'member',
primary key (org_id, user_id)
);
create table public.projects (
id uuid primary key,
org_id uuid not null references public.orgs(id),
name text not null
);
alter table public.projects enable row level security;
create policy "members read projects"
on public.projects
for select
to authenticated
using (
exists (
select 1 from public.org_members m
where m.org_id = public.projects.org_id
and m.user_id = (select auth.uid())
)
);
create policy "members insert projects"
on public.projects
for insert
to authenticated
with check (
exists (
select 1 from public.org_members m
where m.org_id = NEW.org_id
and m.user_id = (select auth.uid())
)
);
Policies run as the caller (SECURITY INVOKER by default), which is what makes them testable per identity. auth.uid() reads the caller's JWT sub claim.
Test 1: the anonymous key sees nothing
With no JWT (the anon role), a plain SELECT returns:
permission denied for table projects
Not an empty result — a permission error. That is the correct boundary.
Test 2: same org, two members see the same rows
Two different Acme users each see exactly Acme's two projects:
rows_visible
2
Row-level security is per-request, not per-session cache.
Test 3: cross-org insert is blocked
A Globex member tries to insert a project into Acme's org_id:
new row violates row-level security policy for table "projects"
WITH CHECK gates the row being produced, so a user cannot write into another tenant's org. This is the part tutorials most often skip.
Test 4: same-org read does not leak other tenants
A Globex owner queries select count(*) from projects and receives Globex's rows only. Identity is bound to the request, not to a client-supplied org_id in the body.
The write-path gap tutorials miss
An update policy that lists only USING (no WITH CHECK) looks safe in a read-heavy demo but behaves differently once a column like org_id changes:
-
USING— which existing rows this statement may touch. -
WITH CHECK— which new row this statement may produce.
For multi-tenant data, both should reference the tenant boundary (org_id via org_members).
Four read-only catalog checks
Run these against a scratch copy of your project. None writes or reads application rows.
-- 1. RLS on every tenant table
select n.nspname, c.relname
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public'
and c.relkind = 'r'
and not c.relrowsecurity;
-- 2. Policies reference auth.uid() (not a client-supplied column)
select tablename, policyname, cmd, qual
from pg_policies
where schemaname = 'public'
and qual not like '%auth.uid()%'
and qual not like '%current_setting(%';
-- 3. INSERT policies have WITH CHECK
select tablename, policyname
from pg_policies
where schemaname = 'public'
and cmd = 'INSERT'
and with_check is null;
-- 4. anon holds no table grants
select table_name, grantee, privilege_type
from information_schema.role_table_grants
where table_schema = 'public'
and grantee = 'anon'
and privilege_type in ('INSERT','UPDATE','DELETE');
Honest limits
Measured against a local PostgreSQL 17 fixture with a hand-written auth.uid() shim — not against a hosted Supabase project. Hosted defaults, the exact revision, and the real JWT your client sends are outside what a local catalog query can show. Zero findings means "no catalog gap I know how to detect", not "your tenant isolation is safe".
The recurring failure is not a missing check — it is that the schema changed after the check. A nightly read-only diff reports only new fingerprints and fails closed when the credential breaks:
https://cenkkurtoglu.com/blog/multi-tenant-org-id-rls-measured
Full write-up with links to the other checklists: https://cenkkurtoglu.com/blog/multi-tenant-org-id-rls-measured
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.