Supabase docs: exposed tables need grants and RLS, and existing tables keep old grants
In short
Supabase wants grants and RLS on every exposed table. Its new default only covers new tables, so check your old ones: one SQL query finds them.
In Supabase, grants and row-level security (RLS) are two separate locks, and the docs ask for both on every exposed table. A grant decides whether a role can touch the table at all. RLS decides which rows it gets [1]. A table in an exposed schema with RLS off is readable and writable by any role that holds a grant on it [2].
Supabase is changing its default so that new tables no longer get those grants automatically [4]. That only helps tables made after the change. Your existing tables keep their grants and stay reachable through the Data API [5], so they need a check of their own. This post shows the query I use for it, run on Supabase's own Postgres image, and how Strongy, a workout app I build with Ilkin Guluzada, keeps its tables closed.
What the Supabase docs say about grants and RLS
The API security guide is short on this point: grants control whether a role can access an object, RLS controls which rows, and you use both for every exposed object [1]. The RLS page says to enable RLS on every table in an exposed schema [2]. Once it's on, the publishable key gets no data through the API until you write policies [3]. So turning RLS on closes the table, and each policy opens a specific part of it again.
The default change is the part that's easy to misread. It revokes the automatic grants so that exposure is something you opt into [4], but like any change to default privileges in Postgres, it applies only to new objects [5]. A project that is a year old has a year of tables made under the old rule.
On testing, the RLS page is blunt: until the test suite passes, you don't know whether the policies do what you intended [6].
See the old default on a local database
The Supabase Postgres image I have locally, public.ecr.aws/supabase/postgres:17.11.0.004, still ships the old default: every new table in public gives anon and authenticated full rights. To see what the change reaches, I made one table, revoked the default grants for new tables, and made a second one.
-- A table made under the old default.
create table public.old_notes (id bigint primary key, user_id uuid not null, body text);
-- The platform change: new tables get no automatic grants.
alter default privileges for role postgres in schema public
revoke all on tables from anon, authenticated;
-- A table made after it.
create table public.new_notes (id bigint primary key, user_id uuid not null, body text);This query lists every table in public, whether RLS is on, and what anon (the role behind the publishable key) can do with it:
-- Every table in an exposed schema: is RLS on, and what can anon do with it?
select c.relname as table_name,
c.relrowsecurity as rls_on,
coalesce(string_agg(g.privilege_type, ', ' order by g.privilege_type), '-') as anon_can
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
left join information_schema.role_table_grants g
on g.table_schema = n.nspname and g.table_name = c.relname and g.grantee = 'anon'
where n.nspname = 'public' and c.relkind in ('r', 'p')
group by c.relname, c.relrowsecurity
order by c.relname; table_name | rls_on | anon_can
------------+--------+---------------------------------------------------------------
new_notes | f | -
old_notes | f | DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE
(2 rows)The old table still lets anon read, change and delete every row: RLS is off and the grants survived the change. The new table has no grants, but RLS is off there too, so the first grant anyone adds later opens it completely. Both rows break the docs' rule, for different reasons.
Close an old table
The fix is RLS, a policy for each thing users are allowed to do, and grants cut down to what those policies need. For a table that users only read:
alter table public.old_notes enable row level security;
create policy "own rows: select" on public.old_notes for select to authenticated
using (user_id = (select auth.uid()));
revoke all on table public.old_notes from public, anon, authenticated;
grant select on table public.old_notes to authenticated; table_name | rls_on | anon_can
------------+--------+----------
new_notes | f | -
old_notes | t | -
(2 rows)Put the policy and enable row level security in the same migration. If you enable RLS first and add policies later, the table returns nothing to your app in between.
Check it in CI
Reading the audit by hand works once. To keep it fixed, this block fails the migration step whenever any table in public has RLS off:
do $$
declare
missing text;
begin
select string_agg(c.relname, ', ' order by c.relname) into missing
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public' and c.relkind in ('r', 'p') and not c.relrowsecurity;
if missing is not null then
raise exception 'RLS is off on: %', missing;
end if;
end
$$;On the database above, with new_notes still open, psql -v ON_ERROR_STOP=1 -f check-rls.sql prints this and exits with status 3. After alter table public.new_notes enable row level security it prints DO and exits with 0.
ERROR: RLS is off on: new_notes
CONTEXT: PL/pgSQL function inline_code_block line 11 at RAISEIf you expose more schemas than public, add them to the where clause.
How Strongy keeps its tables closed
Strongy is a workout tracker for iPhone and Apple Watch. The phone's own database holds your training, and a Supabase backend backs it up and syncs it. Every synced table gets the same treatment in its migration: RLS on, four policies (select, insert, update, delete) that match user_id to auth.uid(), then revoke all from public, anon and authenticated, and a grant back to authenticated of only those four operations. A comment in the first migration says why: Supabase's default privileges grant everything to anon, so the migration takes them back explicitly.
A migration doesn't prove anything by itself, so a pgTAP file tests the result: 144 checks, each run inside a transaction that is rolled back. Two of them are the docs' rules written as tests: RLS is enabled on every table in public, and anon has no privileges on any table there. The rest sign in as one user and try to read, change, delete and forge another user's rows, table by table. Because these tests read the database as it is, not the migration files, they would also catch a table that kept old grants.
What to do
- Run the audit query on each Supabase project, and look at every row where
rls_onisforanon_canisn't empty. - For each old table, enable RLS and write its policies in one migration, then revoke the grants the policies don't need.
- Enable RLS on new tables too, even though they now start with no grants.
- Add
check-rls.sqlto CI, so a table without RLS fails the build. - Test the policies by signing in as one user and reaching for another user's rows. Until those tests pass, you don't know the policies work [6].
Sources
- supabase.com/docs/guides/api/securing-your-apiGrants control whether a role can access an object. RLS controls which rows the role can access. Use both controls for every exposed object.
- supabase.com/docs/guides/database/postgres/row-level-securityA table in an exposed schema without RLS is readable and writable by any role with a grant on it. Enable RLS on every table in an exposed schema.
- supabase.com/docs/guides/database/postgres/row-level-securityOnce RLS is enabled, no data is accessible through the API when using a publishable key, until you create policies.
- supabase.com/docs/guides/api/securing-your-apiSupabase is changing the platform default to revoke these automatic grants so that exposure becomes opt-in.
- supabase.com/docs/guides/api/securing-your-apiChanges to default privileges, including the platform default change, apply only to new objects. Existing tables keep their grants and stay reachable through the Data API.
- supabase.com/docs/guides/database/postgres/row-level-securityUntil the suite passes, you don't know whether the policies do what you intended.