What is the quick way when the role owns nothing?
sudo -u postgres dropuser olduser # or in psql: DROP ROLE IF EXISTS olduser;
In PostgreSQL, users and groups are both roles; a "user" is simply a role with the LOGIN attribute. If the role owns no tables, schemas or functions and holds no privileges, the command finishes instantly. Most of the time, though, an application role owns things, and PostgreSQL protects them.
Why does PostgreSQL say the role cannot be dropped?
The full message reads like ERROR: role "olduser" cannot be dropped because some objects depend on it, followed by details such as "owner of table invoices" or "privileges for schema public". PostgreSQL will not leave tables without an owner or keep grants pointing at a role that no longer exists. Objects and privileges can live in several databases, while roles are cluster-wide, so cleanup must happen in each database the role touched.
How do I stop the role from logging in during cleanup?
ALTER ROLE olduser NOLOGIN; SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE usename = 'olduser';
The first line blocks new connections; the second ends current ones. If an application still uses the role, it now fails, which is a useful last warning before anything is deleted.
How do I hand objects to another role?
Connect to each database and reassign everything owned by the old role, then remove its remaining privileges:
\c crm REASSIGN OWNED BY olduser TO crm_owner; DROP OWNED BY olduser; \c analytics REASSIGN OWNED BY olduser TO analytics_owner; DROP OWNED BY olduser;
REASSIGN OWNED changes the owner of every object in the current database. DROP OWNED then removes privileges granted to the role and any objects it still owns; after the reassignment, that means only grants. Skip REASSIGN only if you truly want the objects deleted, since DROP OWNED alone deletes tables owned by the role.
How do I find which databases need cleanup?
Loop over all databases and count objects owned by the role:
for db in $(sudo -u postgres psql -Atc "select datname from pg_database where datallowconn"); do n=$(sudo -u postgres psql -d "$db" -Atc "select count(*) from pg_class c join pg_roles r on r.oid=c.relowner where r.rolname='olduser'") echo "$db $n" done
Any database with a non-zero count needs the reassign step. Run DROP OWNED BY in every database anyway, because grants leave no ownership trace in that query.
What about databases the role owns?
A database itself has an owner, and REASSIGN OWNED run inside any database also transfers ownership of databases and other shared objects. To be explicit, run ALTER DATABASE crm OWNER TO crm_owner; before dropping the role.
What does the whole sequence look like?
| Step | Where | Statement |
|---|---|---|
| 1 | Any database | ALTER ROLE olduser NOLOGIN; |
| 2 | Any database | SELECT pg_terminate_backend(pid) ... |
| 3 | Each affected database | REASSIGN OWNED BY olduser TO newowner; |
| 4 | Each affected database | DROP OWNED BY olduser; |
| 5 | Any database | DROP ROLE olduser; |
Is there anything outside the database to clean?
pg_hba.conflines naming the role; remove them and runSELECT pg_reload_conf();..pgpassfiles and application configs that still hold its password.- Replication slots or subscriptions created by that role, listed in
pg_replication_slotsandpg_subscription. - The matching Linux account, if peer authentication tied them together; see Linux user management.
Should I take a backup first?
Yes, at least of the affected databases: sudo -u postgres pg_dump -Fc crm > /var/backups/crm-before-drop.dump. The commands above are safe when run in order, but a mistyped role name in DROP OWNED BY deletes real tables. Creating roles the right way in the first place is covered in Create a PostgreSQL user.
Keep Reading
Create a PostgreSQL user
Create PostgreSQL login roles with createuser or SQL, give them a database and schema, set up scram passwords and the matching pg_hba rule.
OpenDatabase servers
PostgreSQL, MongoDB, ClickHouse and other engines on machines picked by working set, write rate and core count.
OpenRemove a MySQL user
Drop MySQL or MariaDB accounts safely: find every host variant, check live sessions and stored-object definers, lock first, then drop.
OpenLinux user management
The full life of an account on a server: create, group, change, expire, lock, audit and delete, with the files behind each step.
OpenFAQ
Quick Answers
Run DROP ROLE name in psql or dropuser name from the shell, after reassigning or dropping what the role owns.
The role owns objects or holds privileges. Run REASSIGN OWNED BY and DROP OWNED BY in each database first.
Yes, it deletes objects still owned by the role. Run REASSIGN OWNED first to keep them.
No. Roles are cluster-wide, while objects and grants live inside individual databases.
Use pg_terminate_backend on the pids from pg_stat_activity for that user name.