Home / RLS / Owner-scoped RLS policy
Supabase RLS · Policy pattern
How to write an owner-scoped RLS policy
Store user_id on the table and compare it to (select auth.uid()) in one policy per operation: USING for SELECT/DELETE, WITH CHECK for INSERT, and both for UPDATE. Full set below.
01 The rule
Owner-scoped means: a row belongs to one user, and only that user can touch it. Store the owner as user_id and compare it to (select auth.uid()) — the id of the signed-in caller — in every policy. Wrapping it in a subselect lets Postgres cache the value per statement, a real speed win at scale.
02 The full policy set
One policy per operation. Note where each uses USING vs WITH CHECK:
migration.sql
alter table notes enable row level security; -- read: filter to rows you own create policy "select own" on notes for select using (user_id = (select auth.uid())); -- create: the new row must belong to you create policy "insert own" on notes for insert with check (user_id = (select auth.uid())); -- update: you can only touch your rows (USING) and can't reassign them (WITH CHECK) create policy "update own" on notes for update using (user_id = (select auth.uid())) with check (user_id = (select auth.uid())); -- delete: you can only delete your rows create policy "delete own" on notes for delete using (user_id = (select auth.uid()));
02b USING and WITH CHECK on UPDATE
USING decides which rows you may update; WITH CHECK validates the row after your change. Leave WITH CHECK out and Postgres reuses USING for the new row, so with user_id = (select auth.uid()) a user still can't hand their row away. Writing both keeps the intent visible, and the two only need to differ when the rows you may change are wider than what a row may become, like shared or team rows.
Neither clause protects a column it doesn't mention: this policy won't stop a user from flipping is_admin on their own row. For columns like that, grant update only on the columns users may change, e.g. grant update (body) on notes to authenticated;
03 Prove it
user_idSee how to test tenant isolation to turn that into an automated check.
Is your app leaking right now?
Paste your live URL. The scanner reads your page and its JavaScript for a Supabase service_role key, the one leak that skips every policy in this guide.
Catch this before it ships
airlock-rls is a CI gate that fails your build when a table ships exposed or a policy is permissive: the same class of bug, caught in CI against a database with your migrations applied, before it reaches prod.
Or start from nextjs-supabase-starter : auth + a table with RLS + an isolation test, so you start from a pattern that's already sealed.
Grab the Supabase RLS cheat sheet
The golden rules, the footguns that leak in prod, correct policy snippets, and the isolation test — on one page.
FAQ
Can I use one policy for all operations?
You can write a policy FOR ALL, but you lose the USING/WITH CHECK distinction that matters for writes. A policy per operation is clearer and safer — especially so UPDATE gets both clauses.
Where does user_id come from on insert?
Set a column default of (select auth.uid()) so the database fills it, or set it explicitly from the signed-in user on the client. Either way the WITH CHECK guarantees it matches the caller.