Sub topic 16
Dropping Roles with `REASSIGN OWNED` and `DROP OWNED`
Assume you know basic SQL, can connect with psql, and understand that a PostgreSQL server contains databases, schemas, and tables. This chapter focuses on safely removing a role without leaving owned objects or privileges behind.
Why Dropping a Role Requires Cleanup
A PostgreSQL role can own tables, schemas, sequences, functions, and other objects. It can also hold privileges granted by other roles. PostgreSQL refuses to drop a role while those dependencies remain.
Two commands handle the cleanup:
REASSIGN OWNED BY old_role TO new_roletransfers ownership.DROP OWNED BY old_roleremoves objects owned by the role and revokes privileges granted to the role.
REASSIGN OWNED is usually safer because it preserves the objects. Use DROP OWNED when the objects should be deleted.
Both commands operate in the current database. Run them in every database where the role owns objects or has privileges. The role itself is server-wide, but many objects are database-local.
Transfer Ownership and Remove Privileges
Suppose alice is leaving the project, and app_owner should receive her objects. Connect to each relevant database and run:
-- Run in each database where alice may have dependencies
REASSIGN OWNED BY alice TO app_owner;
DROP OWNED BY alice;
-- Run once, after every database has been processed
DROP ROLE alice;REASSIGN OWNED changes the owner of objects owned by alice. It does not remove privileges that alice received from another role.
DROP OWNED BY alice removes privileges granted to alice in the current database. It also drops objects still owned by alice, so run it only after ownership has been reassigned or after confirming that deletion is intended.
The commands require suitable authority. A database administrator commonly performs this operation. The replacement role must be able to own the transferred objects.
Common Errors
- Running the commands in only one database. Repeat them wherever the role might have dependencies.
- Confusing ownership with privileges. Transferring ownership does not remove grants.
- Using
DROP OWNEDbefore checking what the role owns. It can delete the role's remaining objects. - Forgetting role membership. A role can belong to other roles, so review memberships before dropping it.
- Treating
DROP OWNEDas dropping the role. It cleans up database objects and privileges but leaves the role untilDROP ROLEruns.
Use catalog queries and psql commands to inspect ownership before changing anything:
SELECT n.nspname AS schema_name,
c.relname AS object_name,
c.relkind AS object_type,
r.rolname AS owner
FROM pg_class AS c
JOIN pg_namespace AS n ON n.oid = c.relnamespace
JOIN pg_roles AS r ON r.oid = c.relowner
WHERE r.rolname = 'alice'
ORDER BY schema_name, object_name;In psql, \du displays roles and memberships. \dp displays table privileges. Check each database, because these views show the current database's objects.
Exercise
Which command should you use first when Alice's tables must remain available under the application owner?
Show answer
Run REASSIGN OWNED BY alice TO app_owner in each affected database, then run DROP OWNED BY alice to remove Alice's remaining grants before running DROP ROLE alice.