PostgreSQL DROP ROLE Statement
Summary: in this tutorial, you will learn how to use the PostgreSQL DROP ROLE
statement to remove a role.
Introduction to PostgreSQL DROP ROLE statement
The DROP ROLE
statement allows you to delete a role:
In this syntax:
- First, specify the name of the role that you want to remove after the
DROP ROLE
keywords. - Second, use the
IF EXISTS
option to conditionally remove the role only if it exists.
To remove a superuser role, you need to be a superuser. To drop non-superuser roles, you need to have the CREATEROLE
privilege.
When you remove a role referenced in any database, PostgreSQL will raise an error. In this case, you need to take two steps:
- First, either remove the database objects owned by the role using the
DROP OWNED
statement or reassign the ownership of the database objects to another roleREASSIGN OWNED
. - Second, revoke any privileges granted to the role.
The REASSIGN OWNED
statement reassigns the ownership of all dependent objects of a target role to another role. Because the REASSIGN OWNED
statement can only access objects in the current database, you need to execute this statement in each database that contains objects owned by the target role.
After transferring the ownership of objects to another role, you need to drop any remaining objects owned by the target role by executing the DROP OWNED
statement in each database that contains objects owned by the target role.
In other words, you should execute the following statements in sequence to drop a role:
PostgreSQL DROP ROLE statement example
Let’s take an example of how to use the DROP ROLE
statement:
- First, create a new role called
alice
- Second, use the
alice
role to create a table calledcustomers
. - Third, remove the role
alice
.
We’ll use the psql client tool. But, you can use any client tool of your choice.
Step 1. Setting a new role and database
First, open the Command Prompt on Windows or Terminal on Linux and log in to PostgreSQL using the postgres
role:
Second, create a new database called sales:
Third, create a new role called alice
:
Replace the Password
with the actual one.
Fourth, grant createdb
privilege to alice
:
Fifth, grant all privileges on the sales
database to alice
:
Sixth, switch the current database to sales:
Sixth, grant all privileges of the public
schema database to alice
:
Finally, exit the current session:
Step 2. Using the new role to create database objects
First, log in to the PostgreSQL server using the alice
role:
Second, create a new table in the sales
database:
Third, show the table list in the sales
database:
Output:
Finally, quit the current session:
Step 3. Removing the role alice
First, log in to the PostgreSQL server using the postgres
role:
Second, attempt to drop the role alice
:
PostgreSQL issued the following error:
The role alice
cannot be dropped because it has dependent objects.
Third, switch to the sales
database:
Fourth, reassign owned objects of alice
to postgres
:
Fifth, drop owned objects by alice
:
Sixth, drop the role alice
:
Seventh, list the current roles:
You will see that the role alice
has been removed.
Finally, quit the current session:
Summary
- Use the PostgreSQL
DROP ROLE
statement to remove a role. - If a role has dependent objects, use the
REASSIGN OWNED
andDROP OWNED
statements in sequence to remove dependent objects of the role before executing theDROP ROLE
statement.