The Wayback Machine - https://web.archive.org/web/20240618222400/https://www.geeksforgeeks.org/postgresql-drop-role/
Open In App

PostgreSQL – DROP ROLE

Last Updated : 16 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In PostgreSQL the DROP ROLE statement to remove a role.

Syntax: DROP ROLE [IF EXISTS] target_role;

Let’s analyze the above syntax:

  • After the DROP ROLE keywords add the role name that is to be deleted.
  • Use the IF EXISTS option can be used to issue a notice instead of an error when a request to remove a role that does not exist is made.

it is important to note that to remove a superuser role, one needs to be a superuser. To remove a non-superuser roles, you will need the privilege to create a role using the CREATE ROLE statement. If we accidentally try to remove a role that has been referenced in any database, PostgreSQL raises an error. In this case, one has to take two steps:

  • You either need to remove the database objects owned by that particular role using the DROP OWNED statement or reassign the ownership of the database objects to another role using the REASSIGN OWNED statement.
  • You will also be needing to revoke all other permissions granted to the role.

In other words, follow the below statements exactly as shown to drop a role:

REASSIGN OWNED BY target_role TO another_role;
DROP OWNED BY target_role;


DROP ROLE target_role;

Example:

In this example:

  • Here, we will create a new role called geeksforgeeks and use this role to create a table named employees.
  • Then, we will show the step by step guide on how to remove the role geeksforgeeks from the PostgreSQL database server.

First, log in to PostgreSQL using the Postgres role:

psql -U postgres

Second, create a new role called geeksforgeeks using the below statement:

create role geeksforgeeks with login password 'geeks12345';

Third, grant createdb privilege to geeksforgeeks using the below statements:

alter role geeksforgeeks createdb;

Fourth, create a new database called sales:

create database sales;

Now, login to the PostgreSQL database server using the geeksforgeeks role as shown below:

psql -U geeksforgeeks -W sales

Add a new table to the Sales database:

create table employee(
    customer_id int generated always as identity,
    customer_name varchar(150) not null,
    primary key(employee_id)
);

Now show the list of tables in the sales database using the below command:

\du

This will show you the following result:

Now log in as Postgres and attempt to drop the geeksforgeeks role:

drop role geeksforgeeks;

Output:


Previous Article
Next Article

Similar Reads

PostgreSQL - DROP DATABASE
In PostgreSQL the DROP DATABASE statement is used to delete an existing database. It deletes the cataloged entries and data directory completely. It is an irreversible process. It can be performed by only the database owner and before executing this statement all connections to the database must be terminated. Syntax: DROP DATABASE [IF EXISTS] name
2 min read
PostgreSQL - DROP COLUMN
PostgreSQL also supports the DROP COLUMN clause in the ALTER TABLE statement to remove one or more columns of a table. When you remove a column from a table, PostgreSQL will automatically remove all of its indexes and constraints involving the column. Syntax: ALTER TABLE table_name DROP COLUMN column_name; Now let's look into an example. Example: F
2 min read
PostgreSQL - DROP TABLE
PostgreSQL has a DROP TABLE statement that is used to remove an existing table or tables from the database. Syntax: DROP TABLE [IF EXISTS] table_name [CASCADE | RESTRICT]; Let's analyze the above syntax: We specify the table name after the DROP TABLE keyword to remove the table permanently from the database. In case the table that you want to remov
2 min read
PostgreSQL - DROP SCHEMA
PostgreSQL also supports the deletion of a schema and its objects using the DROP SCHEMA statement. Syntax: DROP SCHEMA [IF EXISTS] schema_name [ CASCADE | RESTRICT ]; Let's analyze the above syntax: First, specify the name of the schema from which you want to remove after the DROP SCHEMA keywords. Second, use the IF EXISTS option to conditionally t
2 min read
PostgreSQL - Drop Procedure
In PostgreSQL, the drop procedure statement removes a stored procedure. Syntax: drop procedure [if exists] procedure_name (argument_list) [cascade | restrict] Let's analyze the above syntax: First, specify the name (procedure_name) of the stored procedure that you want to remove after the drop procedure keywords.Second, use the if exists option if
4 min read
PostgreSQL - Drop Function
In PostgreSQL, the Drop function statement is used to remove a function. Syntax: drop function [if exists] function_name(argument_list) [cascade | restrict] Let's analyze the above syntax: First, specify the name of the function that you want to remove after the drop function keywords.Second, use the if exists option if you want to instruct Postgre
3 min read
PostgreSQL - DROP TRIGGER
In PostgreSQL, the DROP TRIGGER statement is used to drop a trigger from a table. Syntax: DROP TRIGGER [IF EXISTS] trigger_name ON table_name [ CASCADE | RESTRICT ]; Let's analyze the above syntax: First, specify the name of the trigger which you want to delete after the DROP TRIGGER keywords.Second, use IF EXISTS to conditionally delete the trigge
2 min read
PostgreSQL - DROP INDEX
In PostgreSQL, the DROP INDEX statement to remove an existing index. Syntax: DROP INDEX [ CONCURRENTLY] [ IF EXISTS ] index_name [ CASCADE | RESTRICT ]; Let's analyze the above syntax: index_name : This is used to specify the name of the index that you want to remove after the DROP INDEX clause.IF EXISTS: Attempting to remove a non-existent index w
2 min read
PostgreSQL - DROP TABLESPACE
In PostgreSQL, the DROP TABLESPACE statement is used to remove a tablespace. Syntax: DROP TABLESPACE [IF EXISTS] tablespace_name; Let's analyze the above syntax: First, specify the name of the tablespace that is to be deleted after the DROP TABLESPACE keywords.Second, use the IF EXISTS option to handle errors in case the tablespace doesn't exists.
2 min read
PostgreSQL - DROP VIEWS Statement
A view can be seen as a virtual table that can contain all rows of a table or selected rows from one or more tables. These allow us to see limited data only instead of complete information stored in a table. A view can be created from one or many Base Tables (the table from which view is created). We can perform various operations on views such as
3 min read