Removing a user without dropping their data
When trying to drop a user who owns some tables or other database objects, you get the following error, preventing the user from being dropped:
testdb=# drop user bob;
ERROR: role "bob" cannot be dropped because some objects depend on it
DETAIL: owner of table bobstable
owner of sequence bobstable_id_seq
This recipe presents two solutions to this problem.
Getting ready
To modify users, you must either be a superuser or have the CREATEROLE
privilege.
How to do it…
The easiest solution to this problem is to refrain from dropping the user and use the trick from the Temporarily preventing a user from connecting recipe, which prevents the user from connecting:
pguser=# alter user bob nologin;
ALTER ROLE
This has the added benefit of the original owner of the table being available later, if needed, for auditing or debugging purposes (Why is this table here? Who created it?).
Then, you can...