Create a table in Supabase, and it may already have privileges for anon, authenticated and service_role. You never wrote a grant. They come from the default privileges of the role that created the table.
My client portal handled this the slow way: every migration revoked them, table by table. That works until one migration forgets. After a reader asked about it on a post about my RLS review, I tried to close the defaults instead. It took one migration, but two things surprised me on the way.
Why it matters (and why it's usually not a leak)
Privileges decide whether a role can touch a table at all. RLS decides which rows it sees. If RLS is enabled, extra privileges alone don't expose any rows.
The risk is the table where RLS was forgotten. Then the privileges are the only thing left, and they're already open. Closing the defaults means a forgotten grant fails loudly with "permission denied" instead of quietly working.
Step 1: read what new objects actually get
This is read-only. You can run it in the SQL Editor:
select pg_get_userbyid(d.defaclrole) as owner_role, coalesce(n.nspname, '(all schemas)') as schema, case d.defaclobjtype when 'r' then 'tables' when 'S' then 'sequences' when 'f' then 'functions' end as object_type, case when a.grantee = 0 then 'PUBLIC' else pg_get_userbyid(a.grantee) end as grantee, string_agg(a.privilege_type, ', ' order by a.privilege_type) as privileges from pg_default_acl d left join pg_namespace n on n.oid = d.defaclnamespace cross join lateral aclexplode(d.defaclacl) a group by 1, 2, 3, 4 order by 1, 2, 3, 4; Surprise 1: local and production didn't match
I almost wrote the migration from my local results alone. Then I ran the same query in production first. For tables created by postgres in public:
| anon / authenticated / service_role get | |
|---|---|
| Local Supabase | all 8 privileges (SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER, MAINTAIN) |
| Hosted project | 4 (TRUNCATE, REFERENCES, TRIGGER, MAINTAIN) |
Sequences differed too: locally the three roles got USAGE, SELECT and UPDATE; in production only postgres had them. I don't know why they differ. It may be the project's age or a change on Supabase's side. What I learned: check the environment you're actually changing, not the one on your laptop.
Surprise 2: FOR ROLE, and the role you can't change
ALTER DEFAULT PRIVILEGES only applies to objects created by one role. Without FOR ROLE, that's whoever runs the statement. So the migration names the role explicitly:
alter default privileges for role postgres in schema public revoke all on tables from anon, authenticated, service_role; alter default privileges for role postgres in schema public revoke all on sequences from anon, authenticated, service_role; alter default privileges for role postgres in schema public revoke execute on functions from anon, authenticated, service_role; Supabase has a second role that creates objects, supabase_admin. Its defaults are open too. Trying to close them from postgres fails:
ERROR: 42501: permission denied to change default privileges postgres isn't a member of supabase_admin, so there's nothing to do from a migration. I left those defaults alone and kept the per-table revokes as a second layer.
The part I couldn't close: functions
New functions get EXECUTE for PUBLIC (everyone) from a built-in Postgres default. It doesn't show up in pg_default_acl, and revoking it inside one schema doesn't remove it.
Removing it needs a default change across all schemas. Locally that worked fine. But production showed that postgres owns some extensions (pgcrypto, uuid-ossp, pg_stat_statements), and nothing grants their functions back in the extensions schema. So a global revoke could make functions from a future extension uncallable from the app. I didn't ship that part. New functions still get a per-function revoke, like is_admin() in my post on the 42P17 recursion error.
How I checked it
The verify script creates a throwaway table, sequence and function inside a transaction that always rolls back, then reads their privileges. Nothing is left behind.
- Before the migration, the probe failed (the defaults were open). That's the check working.
- After it, the new table and sequence had no privileges for
anon,authenticatedorservice_role. The function only hadEXECUTEforPUBLIC, as expected. - Existing grants didn't change. I took fingerprints of every table, column and function grant before and after, in production too. All four matched.
- The same checks run on every pull request in GitHub Actions: 482 pass. Rolling the migration back makes 12 of them fail, so they really test the migration.
The migration also ends in the same shape in both environments, even though they started differently.
Quick checklist
- Run the query above in production, not only locally. Look at the rows for
postgresinpublic. - Close tables and sequences with
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public. - Keep per-table
revoke/grantin every migration anyway.supabase_admin's defaults stay open. - For functions, revoke
EXECUTEone function at a time. - Test it with throwaway objects in a transaction that rolls back.
The pull request with the migration, verify and rollback SQL: https://github.com/kuros-works/client-portal-demo/pull/24
Want someone to check things like this in your Supabase project? I review RLS policies and privileges as a fixed-price job on Upwork: https://www.upwork.com/services/product/development-it-a-supabase-rls-security-review-for-your-lovable-bolt-or-cursor-app-2126067179511646761
