Row-Level Security, Explained
Row-level security makes Postgres filter every query by who is asking. It protects an app only when it is on, has a policy per command, and nothing bypasses it.
5 min read / Updated / Last checked
- Format
- Explainer · Intermediate
- Who it is for
- Founders and the builders they hire
- You leave with
- Read and write basic Postgres and Supabase row policies, recognize the common mistakes, and run a test that proves one user cannot reach another's rows.
The problem
In September 2025, Wiz Research published what it found while looking at apps built on vibe coding platforms. Among the recurring problems were database tables left exposed by weak or missing row-level security: developers "often implement overly permissive RLS policies or forget to enable them at all", in Wiz's words. One case it described was an enterprise game that leaked users' personal data and IP addresses [1].
The reason this keeps happening is architectural. Many AI-built apps, especially on Supabase, let the browser query the database directly through an API. The browser holds a public key that anyone can copy. Between that key and every customer's data there is one control: the row policies on each table. If a policy is missing, too broad, or bypassed, any user can read or change any row, and nothing in the app's screens will show it.
Founders hear "we have RLS" and treat it as a yes. It is a set of rules, one per table and per command, and it is only as good as the weakest of them. The goal of this explainer is to make those rules readable, so a founder can ask the right questions and a builder can prove the answers.
How it works
Row-level security (RLS) is a PostgreSQL feature that attaches a filter to a table. Once it is enabled, every query against the table, from any client, only sees and changes the rows a policy allows.
Default deny. When RLS is enabled and no policy exists, Postgres applies a default-deny policy: no rows are visible or can be changed [2].
Two kinds of check. A policy's USING expression decides which existing rows a user can see, update or delete. Rows that fail it are silently skipped, with no error. A WITH CHECK expression decides which new or changed rows may be written, and a row that fails it raises an error [5]. If an update policy has only USING, Postgres uses it for both [5].
One policy per command. Policies apply to SELECT, INSERT, UPDATE, DELETE or ALL. An update that reads columns, in a WHERE clause for example, also needs a select policy on the same rows [5].
Who is asking. In Supabase, the browser sends the publishable key and, after login, the user's token. Postgres runs the query as the anon or authenticated role, and auth.uid() returns the user id from the token. With no session, auth.uid() returns null, and a check such as auth.uid() = user_id quietly matches nothing [3].
Who skips it. Superusers and roles with the BYPASSRLS attribute always bypass RLS, and table owners do too unless the table uses FORCE ROW LEVEL SECURITY [2]. In Supabase, secret keys run as the service_role role, which bypasses RLS, so they must never reach a browser [3].
Combining. Several permissive policies on one table combine with OR, so one broad policy opens the table even if the others are strict. Restrictive policies combine with AND [2].
How a browser query meets a row policy
Fig. 01- Browserpublishable key + user token
- Data APIruns as authenticated or anon
- Row policyUSING and WITH CHECK per row
- Tableonly permitted rows
By the numbers
- for a policy query on a 100K-row table once
user_idwas indexed, in Supabase's tests [4] - 171 ms to under 0.1 ms
- after wrapping
auth.uid()in aselectso it runs once per query [4] - 179 ms to 9 ms
- for a role-check function policy after the same change [4]
- 178,000 ms to 12 ms
- improvement Supabase reports from indexing policy columns on large tables [4]
- 100x+
| Mistake | What happens | Advisor check | Fix |
|---|---|---|---|
| RLS off on an exposed table | Anyone with the project URL can access it | 0013 rls_disabled_in_public | Enable RLS, add policies |
Policy with using (true) | Every row is open to that role | 0024 permissive_rls_policy | Compare with the owner column |
Policy reads user_metadata | Users can edit their own metadata and grant themselves access | 0015 rls_references_user_metadata | Use app_metadata or a roles table [3] |
| View created by the owner role | The view ignores the table's policies | 0010 security_definer_view | security_invoker = true on Postgres 15+ [3] |
auth.uid() called per row | Slow queries on large tables | 0003 auth_rls_initplan | (select auth.uid()) [4] |
| RLS on, no policies | The API returns nothing | 0008 rls_enabled_no_policy | Add a policy per command |
The performance numbers matter for security too. A policy is an expression Postgres evaluates for each row before the query's own conditions [2]. On a 100,000-row table, an unindexed policy took 171 ms, and a policy calling a role-check function per row took about three minutes before the fix [4]. Slow policies tempt builders to "temporarily" loosen them. Indexing the policy column and wrapping auth.uid() in a select keep strict policies fast.
Implementation
The migration below protects a notes table where each row belongs to one user. It is written for Supabase, where the auth schema, auth.uid() and the anon and authenticated roles already exist.
-- migration: notes owned by one user, readable and writable only by that user
create table public.notes (
id bigint generated always as identity primary key,
user_id uuid not null default auth.uid() references auth.users (id) on delete cascade,
body text not null,
created_at timestamptz not null default now()
);
create index notes_user_id_idx on public.notes (user_id);
alter table public.notes enable row level security;
create policy "notes: owner can read" on public.notes
for select to authenticated
using ((select auth.uid()) = user_id);
create policy "notes: owner can insert" on public.notes
for insert to authenticated
with check ((select auth.uid()) = user_id);
create policy "notes: owner can update" on public.notes
for update to authenticated
using ((select auth.uid()) = user_id)
with check ((select auth.uid()) = user_id);
create policy "notes: owner can delete" on public.notes
for delete to authenticated
using ((select auth.uid()) = user_id);Read it line by line. The index makes every policy check cheap [4]. Each command gets its own policy, scoped to authenticated, so anonymous requests match no policy and see nothing. (select auth.uid()) runs once per query rather than once per row [4]. The update policy has both USING (which rows you may target) and WITH CHECK (what the row may look like afterwards), so a user cannot move their note into someone else's name [5]. The default auth.uid() on user_id means the browser never has to send it.
Two rules sit outside the file. Keep secret keys on the server, since they bypass every policy here [3]. And create any view on this table with (security_invoker = true). We checked this locally: a plain view over notes returned both users' rows to user A, while the same view with security_invoker returned only A's.
Test it
Run this test on a local or branch database, never against real customer data. It seeds two users, acts as each the way the API does, and fails loudly on any leak.
-- rls_test.sql: run with psql -v ON_ERROR_STOP=1 against a local or branch database
begin;
insert into auth.users (id, email) values
('00000000-0000-0000-0000-00000000000a', 'a@test.local'),
('00000000-0000-0000-0000-00000000000b', 'b@test.local');
insert into public.notes (user_id, body) values
('00000000-0000-0000-0000-00000000000a', 'note of A'),
('00000000-0000-0000-0000-00000000000b', 'note of B');
-- 1. Act as user A, the way the API does after login
set local role authenticated;
select set_config('request.jwt.claims',
'{"sub":"00000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
do $$ begin
assert (select count(*) from notes) = 1, 'A must see exactly one note';
assert not exists (select 1 from notes where body = 'note of B'), 'A can read B';
end $$;
-- 2. A tries to change and delete B's note: both must touch 0 rows
do $$ declare n int; begin
update notes set body = 'changed by A' where body = 'note of B';
get diagnostics n = row_count;
assert n = 0, 'A updated a note of B';
delete from notes where body = 'note of B';
get diagnostics n = row_count;
assert n = 0, 'A deleted a note of B';
end $$;
-- 3. A tries to write a note in B's name: must fail with SQLSTATE 42501
do $$ begin
insert into notes (user_id, body) values ('00000000-0000-0000-0000-00000000000b', 'forged');
raise exception 'forged insert was allowed';
exception when insufficient_privilege then null;
end $$;
-- 4. No session at all: the anon role sees nothing
reset role;
set local role anon;
select set_config('request.jwt.claims', '', true);
do $$ begin
assert (select count(*) from notes) = 0, 'anon can read notes';
end $$;
rollback;Run it with psql -v ON_ERROR_STOP=1 -f rls_test.sql. Expected result: the script ends with ROLLBACK and no ERROR line. We ran the migration and this test on PostgreSQL 17, with a small stand-in for Supabase's auth schema, and it passed. We then broke the policies on purpose: a using (true) select policy failed check 1, a with check (true) insert policy failed check 3, and an open policy for anon failed check 4. Add it to CI so it runs on every migration.
Then list everything that needs a second look across the schema. Every row this query returns is a finding to fix or explain:
-- rls_audit.sql: every row returned is a finding to explain or fix
select 'table without RLS' as finding, c.relname as object
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
union all
select 'policy allows every row', tablename || ': ' || policyname
from pg_policies
where schemaname = 'public' and (qual = 'true' or with_check = 'true')
union all
select 'policy reads user_metadata', tablename || ': ' || policyname
from pg_policies
where schemaname = 'public'
and (qual ilike '%user_metadata%' or with_check ilike '%user_metadata%')
union all
select 'view runs as its owner', c.relname
from pg_class c join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public' and c.relkind = 'v'
and not coalesce(c.reloptions && array['security_invoker=true', 'security_invoker=on'], false)
union all
select 'role bypasses RLS', rolname
from pg_roles
where rolbypassrls and rolname not in ('postgres', 'service_role', 'supabase_admin');On a clean schema it returns no rows. When we added an exposed table with a using (true) policy, a policy reading user_metadata, a plain view and a role with BYPASSRLS, it reported all four. On Supabase, the Security Advisor in the dashboard runs these checks and more [6].
Decision
Treat RLS as required on every table the browser can reach, and as proven only when the two-user test passes.
- Enable RLS and write per-command policies in the same migration that creates the table. Never ship a table without them.
- Run the test above in CI, and the audit query before each release. Ask your builder for both outputs.
- If the app needs rules that are hard to express per row, such as approvals or billing state, move those writes behind your own server API and keep RLS as the second wall.
- For the wider launch check, see Vibe-Code Security Checklist. For designing owner columns before code is generated, see Database Design Before AI Code Generation.
Sources
- [1]Wiz Research Finds Risks in 20% of Vibe-Coded Apps (opens in a new tab)Wiz Research, September 2025
- [2]Row Security Policies (opens in a new tab)PostgreSQL 18 Documentation, November 2026
- [3]Row Level Security (opens in a new tab)Supabase Docs, November 2026
- [4]RLS Performance and Best Practices (opens in a new tab)Supabase Docs, November 2026
- [5]CREATE POLICY (opens in a new tab)PostgreSQL 18 Documentation, November 2026
- [6]Advisors (opens in a new tab)Supabase Docs, November 2026
Technical guidance, not legal or security certification.
Related resources
Need a technical review of an AI-built MVP?
Discuss your build