How to delete a Postgres role
Fix "role cannot be dropped because some objects depend on it". How to reassign or drop a role's objects and privileges in every database first, and which roles never to drop.
What you see
ERROR: role "reporting" cannot be dropped because some objects depend on it
DETAIL: privileges for schema public
privileges for table public.invoices
owner of view public.monthly_revenue
3 objects in database p_app_two
Why it happens
Postgres won't drop a role while anything still refers to it: objects it owns, privileges granted to it, default privileges set for it. The last line matters most: roles belong to the whole Postgres server, not to one database, so the role can own objects or hold grants in databases other than the one you're connected to. DROP ROLE checks all of them.
How to fix it
Check you have the right role. Never drop roles the stack depends on:
postgres,anon,authenticated,service_role,authenticator,pgbouncer,dashboard_user, anything starting withsupabase_orpgsodium, andsh_readonly. Dropping one of these breaks the API, Auth, Storage or the dashboard.See what the role owns in the current database:
select n.nspname as schema, c.relname as name, c.relkind as kind
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where c.relowner = 'reporting'::regrole;
- Move or remove its objects and privileges. In each database the error mentions, run:
-- hand anything it owns to postgres (keeps your tables and views)
reassign owned by reporting to postgres;
-- remove its remaining privileges and default privileges here
drop owned by reporting;
reassign owned comes first on purpose. drop owned on its own also drops every object the role owns, which is rarely what you want for tables.
- Drop the role:
drop role reporting;
If the error still lists objects "in database p_something", repeat step 3 in that database.
Common snags
- "permission denied to reassign objects" / "must be able to SET ROLE": the role you're connected as needs to be a member of, or have admin rights on, the role being dropped. Usually the role that created it has this.
- Active sessions: an app still connected with the role keeps working until it disconnects. Stop it first, or you may see confusing errors later.
- Default privileges:
alter default privileges ... to reportingentries also block the drop.drop ownedclears them in the current database.
On SupaHost
- All projects on an instance share one Postgres server, and each project is a database named
p_<name>(for examplep_app_two). A role you create in one project's SQL editor exists in every project on the instance, which is why the error can point at another project's database. - To run step 3 in another project, open that project's Studio (Dashboard → that project → Database tab → Open Studio) and use its SQL editor. On Business you can also use
sudo supahost-psql app-twoover SSH. - The built-in roles are shared too, so a change to
anonorauthenticated(dropping, renaming, altering) affects every project on the instance.