How to Delete/Drop a User in PostgreSQL

When a Postgres user is no longer needed, you can delete/drop it via the DROP USER command. In Postgres, the DROP USER command allows us to delete or drop a single user or multiple users simultaneously. The DROP USER command can accept the IF EXISTS option to check the user’s existence before performing any action.

This blog post will present a step-by-step guide on deleting a user in Postgres via practical examples. This post will cover the below-listed aspects of the DROP USER command in Postgres:

  • How to Delete/Drop a Single User in Postgres?
  • How to Delete/Drop Multiple Users in Postgres?

So, let’s start!

How to Delete/Drop a Single User in Postgres?

To drop a single user in Postgres, use the DROP USER command as follows:

DROP USER [IF EXISTS] user_name;

In the above syntax:

- DROP USER is a command used to delete a Postgres user.
- IF EXISTS is an option that checks the existence of a user.
- User_name is a user to be deleted.

Example 1: How Do I Delete a User in Postgres?

Let’s learn how to delete a user in Postgres via the below-given stepwise instructions:

Step 1: Launch SQL Shell

Firstly, open the SQL Shell(psql), and provide the login details, such as user name, superuser password, etc.

img

The above snippet shows that we have successfully logged into Postgres.

Step 2: List Users

To get the list of Postgres users, execute the below-mentioned command:

\du
img

The output shows the list of all Postgres users.

Step 3: DROP USER

Suppose a user named “manager” is no longer needed. So to delete the manager, we will run the DROP USER command as follows:

DROP USER IF EXISTS manager;
img

A user named “manager” has been dropped successfully.

Step 4: Verify User Deletion

Let’s execute the \du command one more time to verify if the specified user has been dropped or not:

\du
img

The above snippet proves that the targeted user has been deleted successfully.

Step 5: Deleting Non-Existing User

If you try to delete a user that doesn't exist will result in an error. However, instead of throwing an error, a notice will be displayed if you specify the IF EXISTS option with the DROP USER statement:

DROP USER IF EXISTS manager;

In the above statement, we tried to delete a user named manager:

img

The output proves that Postgres shows a notice instead of throwing an error.

How to Delete/Drop Multiple Users in Postgres?

Follow the comma-separated syntax for the DROP USER command to delete multiple users in one go:

DROP USER IF EXISTS user_1, user_2, …, user_n;

Example: How Do I Delete Multiple Users in Postgres?

Let’s learn how to delete multiple users in Postgres via the below-given stepwise instructions:

Step 1: List Users

You can get the list of Postgres users by executing the following command:

\du
img

The output shows the list of all Postgres users.

Step 2: DROP Multiple USERS

Suppose we need to delete three users: “std_role”, “teach_role”, and “teacher_role”. To do so, we will run the DROP USER command as follows:

DROP USER IF EXISTS std_role, teach_role, teacher_role;
img

The “DROP ROLE” message in the output proves that the selected users have been deleted successfully.

Step 3: Verify User Deletion

Let’s execute the \du command one more time to verify if the targeted users have been deleted or not:

\du
img

The output snippet authenticates that the selected users have been deleted successfully.

That’s it from this Postgres tutorial!

Conclusion

In Postgres, the DROP USER command is used to delete or drop a single user or multiple users simultaneously. To delete a single user, specify the DROP USER statement followed by the user name to be deleted. To delete multiple users simultaneously, use the comma-separated syntax for the DROP USER command. The DROP USER command can accept the IF EXISTS option to check the user’s existence before performing any action. This blog post has explained how to delete single or multiple users in Postgres through practical examples.