Sub topic 16
Dropping roles with REASSIGN OWNED and DROP OWNED
This chapter assumes you know how object ownership differs from granted privileges, how GRANT and REVOKE work, and how default privileges are stored. It also assumes you can connect to several databases in one cluster with psql. You will learn why DROP ROLE often fails, how to clear a role's dependencies, and how to do it in every database.
Why DROP ROLE refuses
A role cannot be dropped while anything still depends on it. PostgreSQL checks four kinds of dependency:
- Objects the role owns, such as tables, schemas, and functions.
- Privileges granted to the role on any object.
- Default privilege entries created with
ALTER DEFAULT PRIVILEGES FOR ROLE. - Shared objects the role owns, such as databases and tablespaces.
Roles belong to the whole cluster, but ordinary objects belong to one database. A role can therefore own objects in appdb, hold a grant in reports, and own nothing in postgres. DROP ROLE checks every database, not only the one you are connected to.
DROP ROLE alice;ERROR: role "alice" cannot be dropped because some objects depend on it
DETAIL: owner of table shop.scratch
privileges for table shop.ordersThe error lists dependencies in the current database and gives counts for other databases, for example 1 object in database reports. Memberships are the exception. DROP ROLE removes the role's group memberships by itself, and it does not touch the other member roles.
The two cleanup commands
Two commands clear the dependencies, and they do different things.
| Command | What it does | Scope |
|---|---|---|
| `REASSIGN OWNED BY old TO new` | Changes the owner of every object `old` owns to `new` | Current database, plus shared objects such as databases and tablespaces |
| `DROP OWNED BY old` | Drops every object `old` owns, revokes every privilege held by `old`, and removes default privilege entries for `old` | Current database, plus privileges on shared objects |
Both commands work on the current database only. You must connect to each database in turn.
-- Run in each database where the role owns objects or holds privileges
REASSIGN OWNED BY alice TO app_owner;
DROP OWNED BY alice;
-- Then, once, from any database:
DROP ROLE alice;The order matters. REASSIGN OWNED keeps the objects and moves them to a new owner. DROP OWNED deletes the objects that are still owned by the role. If you run DROP OWNED first, you delete alice's tables and their data. If you run REASSIGN OWNED first, DROP OWNED only removes the leftover privileges and default privilege entries.
What each step changes
Suppose alice created shop.scratch and was granted SELECT on shop.orders through a direct grant.
REASSIGN OWNED BY alice TO app_ownermakesapp_ownerthe owner ofshop.scratch. The table and its rows stay in place. The command does not remove alice'sSELECTgrant onshop.orders.DROP OWNED BY alicerevokes thatSELECTgrant. It also deletes anyALTER DEFAULT PRIVILEGES FOR ROLE aliceentries. Alice no longer owns anything, so nothing is dropped.DROP ROLE alicenow succeeds in this database. It still fails if another database has dependencies.
The new owner after REASSIGN OWNED is usually a group role such as app_owner, not another person. That keeps ownership stable when people leave. It matches the owner, read-only, and read-write layout from the previous chapter.
Doing it in every database
You can list the databases that need work with the pg_shdepend catalog. It records dependencies of shared objects, such as roles, on other objects.
SELECT coalesce(db.datname, '(shared objects)') AS database,
count(*) AS dependencies
FROM pg_shdepend s
JOIN pg_roles r ON r.oid = s.refobjid
LEFT JOIN pg_database db ON db.oid = s.dbid
WHERE s.refclassid = 'pg_authid'::regclass
AND r.rolname = 'alice'
GROUP BY 1
ORDER BY 1;A row labeled (shared objects) means the role owns a database or tablespace, or holds a privilege on one. REASSIGN OWNED and DROP OWNED handle these too, whichever database you run them from.
To clean every database in one pass, loop over them from the shell:
#!/usr/bin/env bash
set -euo pipefail
OLD_ROLE=alice
NEW_OWNER=app_owner
for db in $(psql -X -At -c "SELECT datname FROM pg_database WHERE datallowconn"); do
psql -X -d "$db" -v ON_ERROR_STOP=1 \
-v old="$OLD_ROLE" -v new="$NEW_OWNER" <<'SQL'
BEGIN;
REASSIGN OWNED BY :"old" TO :"new";
DROP OWNED BY :"old";
COMMIT;
SQL
done
psql -X -v ON_ERROR_STOP=1 -v old="$OLD_ROLE" <<'SQL'
DROP ROLE :"old";
SQLThe script skips databases that do not allow connections, such as template0. It includes template1. If template1 holds objects owned by the role, every new database would inherit them, so cleaning it matters. The :"old" syntax makes psql quote the value as an identifier. Passing the role name as a variable avoids editing SQL by hand for each run.
Permissions you need
- A superuser can run all three commands without restriction.
- A non-superuser needs the right to act as both roles in
REASSIGN OWNED. In practice that means being a member of the old role and of the new owner role. DROP OWNEDneeds membership in the role being cleaned.DROP ROLEneeds theCREATEROLEattribute, plus superuser status if the target is a superuser. In PostgreSQL 16 and later, a role withCREATEROLEcan only drop roles it has been givenADMINrights over, which includes roles it created.
Common failures
The error persists after cleanup. Another database still has dependencies. Run the pg_shdepend query and repeat the two commands in the database it names.
You are connected as the role you want to drop. A role cannot drop itself while it is the current user. Connect as another role.
The role still has open sessions. Dropping a role does not end its existing connections. End them first so they do not keep working with stale access. Find them in pg_stat_activity by usename, and end them with pg_terminate_backend(pid).
Objects owned by system roles. REASSIGN OWNED refuses to touch objects owned by roles PostgreSQL itself requires, such as the bootstrap superuser. This only matters if you try to reassign away from those roles.
The role granted privileges to others. If alice granted privileges with WITH GRANT OPTION, others' grants depend on hers. After REASSIGN OWNED, the new owner takes over the ownership side. Check the result with \dp or the information_schema views afterward, and re-grant anything that was lost.
Verifying the result
After the role is gone, confirm nothing is orphaned:
SELECT rolname FROM pg_roles WHERE rolname = 'alice';
SELECT n.nspname AS schema, c.relname AS object, r.rolname AS owner
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
JOIN pg_roles r ON r.oid = c.relowner
WHERE n.nspname = 'shop'
ORDER BY c.relname;The first query should return no rows. The second shows the owner of each relation in shop. It should list app_owner for objects alice used to own. Run it in each database that had alice's objects.
Exercise
The role bob owns a table in database appdb and holds a SELECT grant on a table in database reports. You connect to appdb and run REASSIGN OWNED BY bob TO app_owner, then DROP OWNED BY bob, then DROP ROLE bob. What happens, and what do you do next?
Show answer
DROP ROLE fails with an error that bob still has dependencies, including "1 object in database reports". REASSIGN OWNED and DROP OWNED only affect the current database (plus shared objects), so bob's SELECT grant in reports remains. Connect to reports, run REASSIGN OWNED BY bob TO appowner and DROP OWNED BY bob there, then run DROP ROLE bob again. The pgshdepend query shows which databases still have dependencies.
Exercise
Why must REASSIGN OWNED run before DROP OWNED when you want to keep a departing user's tables?
Show answer
DROP OWNED drops every object the role owns in the current database, including tables and their data. REASSIGN OWNED first moves ownership to another role, so DROP OWNED then has only privileges and default privilege entries left to remove.