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, and the user is not 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 a previous recipe to prevent 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 assign the rights of the deleted user to a new user, using the following code:
pguser=# GRANT bob TO bobs_replacement; GRANT