In PostgreSQL, DROP ROLE for a departed contractor fails because some objects depend on it; how do you remove the role safely across the cluster?
answer
- dependencies live per database
- transfer first, then strip
- grants and default entries too
- repeat in every database
basics
~20 sIn every database holding its objects, run REASSIGN OWNED BY the role TO a successor, then DROP OWNED BY the role to revoke its remaining grants and default-privilege entries; only then DROP ROLE, which is cluster-wide.
solid answer
~40 s`DROP ROLE` refuses with SQLSTATE `2BP01` while anything recorded in `pg_shdepend` still references the role: objects it owns, privileges granted to it, default-privilege entries, plus counts for other databases. `REASSIGN OWNED BY contractor_lee TO hr_owner` moves ownership of everything the role owns in the current database, and on its first run also of databases and tablespaces it owns; it leaves privileges granted to the role and its default-privilege entries alone. `DROP OWNED BY contractor_lee` then revokes those privileges and removes those entries, and would drop any objects still owned. Both commands act on the current database only, so repeat the pair in every database, always reassigning first, or `DROP OWNED` destroys the contractor's tables. Finally run `DROP ROLE` once. Choose a team-owned `NOLOGIN` successor, not a personal login or the bootstrap superuser.
code
console · 6 lineshr=# DROP ROLE contractor_lee;
ERROR: role "contractor_lee" cannot be dropped because some objects depend on it
DETAIL: owner of table hr.salary_band_import
privileges for table hr.employees
owner of default privileges on new relations belonging to role contractor_lee in schema hr
1 object in database hr_reportinggo deeper
Recall that a role owning objects cannot simply be dropped, and that REASSIGN OWNED and DROP OWNED exist for exactly this.
Explain what each command touches, why the order matters, and why both must run in every database of the cluster.
Run the offboarding safely: block logins, choose a team successor, handle exceptions with ALTER ... OWNER TO, and read the DETAIL to find missed databases.
Design ownership so people never own production objects, which turns offboarding into a routine DROP OWNED and DROP ROLE across the fleet.
## Why DROP ROLE refuses Roles are cluster-wide, but the things that depend on a role live inside individual databases. PostgreSQL records those links in the shared catalog `pg_shdepend`, and `DROP ROLE` refuses while any remain, with `role "contractor_lee" cannot be dropped because some objects depend on it` and SQLSTATE `2BP01` (`dependent_objects_still_exist`). The `DETAIL` lists dependencies in the current database one per line and summarises other databases as counts: - `owner of table ...`: objects the role owns; - `privileges for table ...`: grants made **to** the role on other roles' objects; - `owner of default privileges on new relations belonging to role ...`: its `ALTER DEFAULT PRIVILEGES` entries; - `N objects in database hr_reporting`: the same kinds of dependency elsewhere. Long lists are cut short with a pointer to the server log, which receives the full list. ## The two commands and what each touches | | `REASSIGN OWNED BY old TO new` | `DROP OWNED BY old` | |---|---|---| | objects `old` owns in the current database | ownership moves to `new` | dropped | | databases and tablespaces `old` owns | ownership moves (first run) | not dropped | | privileges granted to `old` on others' objects | untouched | revoked | | default-privilege entries of `old` | untouched | removed | | other databases | untouched | untouched | The table explains the documented recipe: `REASSIGN OWNED` preserves what is valuable, and `DROP OWNED` mops up what `REASSIGN OWNED` cannot reach. Run them in the other order and `DROP OWNED` deletes the tables, data included. `REASSIGN OWNED` requires membership in both the old and the new role, which a superuser or the platform's administrative login normally has. ## The safe procedure 1. Stop new sessions first: `ALTER ROLE contractor_lee NOLOGIN`. 2. Pick the successor: the team's `NOLOGIN` owner role (`hr_owner`), not a colleague's personal login, which only moves the problem, and not the bootstrap superuser. 3. If some objects belong elsewhere, move those individually with `ALTER TABLE ... OWNER TO` before the bulk step. 4. In **each** database of the cluster where the role has anything, run `REASSIGN OWNED BY contractor_lee TO hr_owner;` then `DROP OWNED BY contractor_lee;`. 5. Run `DROP ROLE contractor_lee;` once, from any database. If it still refuses, the `DETAIL` names the database you missed. ## Seeing what the role owns before you move it The `DETAIL` is a summary, so before step 3 list the objects in each database. A query such as `SELECT relnamespace::regnamespace, relname, relkind FROM pg_class WHERE relowner = 'contractor_lee'::regrole` shows tables, views, sequences and indexes; in psql, `\dt *.*` shows table owners and `\df+ *.*` function owners. Anything that is clearly scratch work can be dropped deliberately, and anything another team relies on can be handed to that team's owner role rather than to `hr_owner`. ## Traps - **Wrong order.** `DROP OWNED` before `REASSIGN OWNED` drops everything the role owns. - **One database only.** Both commands ignore other databases, so a single run leaves dependencies that the next `DROP ROLE` reports as counts. - **CASCADE.** `DROP OWNED ... CASCADE` can recurse into objects owned by other roles that depend on the contractor's objects, such as views over its tables. - **The bootstrap superuser as source.** `REASSIGN OWNED BY postgres` fails because the system's own objects are owned by that role and cannot be reassigned. - **Revoking memberships by hand.** Unnecessary: `DROP ROLE` automatically revokes the role's memberships in other roles and other roles' memberships in it. Membership grants the role made as grantor are different; they are recorded as dependencies, and `DROP OWNED` removes them. ## Why a team owner role pays off All of this is easy when individuals never own production objects. If every migration runs `SET ROLE hr_owner`, people's logins own nothing, and removing a departed engineer comes down to `DROP OWNED` for stray grants and default entries, then `DROP ROLE`. The procedure above is mainly for clusters where ownership has drifted onto personal logins. When a role must stay for audit history, `ALTER ROLE ... NOLOGIN` plus revoking its memberships disables it without touching its objects, and `DROP ROLE` can follow later.
- In PostgreSQL, after REASSIGN OWNED in every database, DROP ROLE still lists privileges for table hr.employees. Why?`REASSIGN OWNED` changes only ownership; it does not touch privileges granted to the role on objects other roles own, nor its default-privilege entries. `DROP OWNED BY contractor_lee` in that database revokes those grants and removes the entries, after which `DROP ROLE` succeeds.
- Why is REASSIGN OWNED BY postgres TO hr_owner a mistake on a PostgreSQL cluster?The bootstrap superuser owns the system catalogs and built-in objects, and the command refuses with an error that those objects are required by the database system. The bootstrap role is never meant to be dropped; reassign individual application objects with `ALTER ... OWNER TO` instead.
- DROP ROLE ... CASCADE removes the role and everything it owns.
- Running REASSIGN OWNED once from any database covers the whole cluster.
- REASSIGN OWNED also revokes the privileges granted to the old role.
- Run DROP OWNED first; REASSIGN OWNED then moves what is left.
- A role's memberships must be revoked by hand before DROP ROLE.