PostgreSQL How to delete a user in PostgreSQL
Deleting a PostgreSQL user is simple and can be done in two ways: using the dropuser utility or by using the DROP USER statement.
If the user you want to drop does not exist, PostgreSQL will throw an error. If you don't want an error, make sure the user is dropped only if it exists:
If you want to delete multiple users at once, list them separated by a comma. Let's extend our example with another user named "luigi":
If the user you want to delete has dependencies, the command will fail. In order to delete a PostgreSQL user with dependencies, you have to reasign its dependencies first, and then execute the delete statement.
1. Delete a PostgreSQL user using the dropuser utility
Let's assume you have a user "mario" you want to delete. Using the dropuser utility, this can be achieved with the following command:sudo -u postgres dropuser mario -e
2. Delete a PostgreSQL user using the DROP USER statement
2.1 Connect to psql
Connect to the psql client with the following command (and replace the user if needed):sudo -u postgres psql
2.2 Delete the user
Now you can run the DELETE USER statement. In this example we assume you have a user "mario" which you want to delete.DROP USER mario;
If the user you want to drop does not exist, PostgreSQL will throw an error. If you don't want an error, make sure the user is dropped only if it exists:
DROP USER IF EXISTS mario;
If you want to delete multiple users at once, list them separated by a comma. Let's extend our example with another user named "luigi":
DROP USER IF EXISTS mario, luigi;
If the user you want to delete has dependencies, the command will fail. In order to delete a PostgreSQL user with dependencies, you have to reasign its dependencies first, and then execute the delete statement.
REASSIGN OWNED BY mario TO postgres;
Looking for a backup solution? Try Weap.io
Simple & flexible backup solution to keep your servers, websites & databases safe.
Start free trial