DROP ROLE in PostgreSQL | Delete Users & Safely Remove Roles
Learn how to use DROP ROLE command to remove users and roles in PostgreSQL. Includes ownership rules, REASSIGN OWNED BY syntax, and safe role deletion methods.
In PostgreSQL, DROP ROLE is used to remove (delete) a role/user from the system.
πΉ What is DROP ROLE?
π It is a command used to:
- Delete a role (user or group)
- Remove its access to the database system
πΉ Why Do We Use DROP ROLE?
β 1. Remove Unused Users
- When a user leaves (e.g., student graduates, employee resigns)
β 2. Improve Security
- Remove unnecessary accounts to prevent misuse
β 3. Clean Database System
- Keep roles organized and manageable
πΉ Basic Syntax
DROP ROLE role_name;πΉ Simple Example (Beginner)
Step 1: Create a Role
CREATE ROLE test_user WITH LOGIN PASSWORD '123';Step 2: Drop the Role
DROP ROLE test_user;βοΈ The role is now removed from PostgreSQL
β οΈ Important Rules (Very Important)
You CANNOT drop a role if it:
β 1. Owns a database
Example:
ALTER DATABASE dvdrental OWNER TO test_user;Then:
DROP ROLE test_user; -- β ERRORβ 2. Owns tables or objects
β 3. Has active connections
πΉ How to Safely Drop a Role
Step 1: Reassign ownership
REASSIGN OWNED BY test_user TO postgres;Step 2: Remove permissions
DROP OWNED BY test_user;Step 3: Drop role
DROP ROLE test_user;πΉ Example with dvdrental
-- Create role
CREATE ROLE trainee WITH LOGIN PASSWORD '123';
-- Give access
GRANT CONNECT ON DATABASE dvdrental TO trainee;
-- Remove role safely
REASSIGN OWNED BY trainee TO postgres;
DROP OWNED BY trainee;
DROP ROLE trainee;π§ Key Concept
π A role cannot be deleted if it is still linked to database objects
π§βπ« Teaching Tip
Ask students: π βWhy did DROP ROLE fail?β
This helps them understand:
- Ownership
- Dependencies
- Security concepts
π Bonus (Check Roles Before Deleting)
SELECT rolname FROM pg_roles;π§ͺ Lab: Understanding DROP ROLE in PostgreSQL
π― Learning Objectives
Students will learn:
- How to safely delete roles
- Why
DROP ROLEmay fail - How to resolve dependency errors
- Difference between ownership and permissions
πΉ Scenario
You are managing a system where temporary users (trainees, interns, students) must be removed after use.
πΉ Step 1: Create a Test Role
CREATE ROLE trainee WITH LOGIN PASSWORD '123';Verify:
SELECT rolname FROM pg_roles;πΉ Step 2: Drop the Role (Basic Case)
DROP ROLE trainee;βοΈ Works because:
- No ownership
- No permissions
- No dependencies
πΉ Step 3: Create Role Again
CREATE ROLE trainee WITH LOGIN PASSWORD '123';πΉ Step 4: Assign Permissions (dvdrental)
GRANT CONNECT ON DATABASE dvdrental TO trainee;Try dropping:
DROP ROLE trainee;π β Ask students:
- Does it work or fail?
βοΈ Usually it fails due to dependencies
πΉ Step 5: Fix the Error
DROP OWNED BY trainee;
DROP ROLE trainee;π₯ Critical Learning Section
πΉ Step 6: Ownership Problem (Very Important)
Create role again:
CREATE ROLE trainee WITH LOGIN PASSWORD '123';Assign ownership:
ALTER DATABASE dvdrental OWNER TO trainee;Now try:
DROP ROLE trainee;β This will FAIL
π§ Why It Fails?
π Because:
- Role owns the database
- PostgreSQL prevents deletion to avoid data loss
πΉ Step 7: Resolve Ownership Issue
REASSIGN OWNED BY trainee TO postgres;Then:
DROP OWNED BY trainee;βRemove everything that this role (trainee) owns in the current database.β
This includes:
- Tables
- Views
- Sequences
- Functions
- Permissions (grants)
Finally:
DROP ROLE trainee;βοΈ Now it works
π Step 8: Investigate Ownership
SELECT datname, pg_catalog.pg_get_userbyid(datdba) AS owner
FROM pg_database;π§ͺ Student Tasks
β Task 1: Basic Deletion
- Create role
temp_user - Drop it immediately
β Task 2: Permission Dependency
- Create role
intern - Grant SELECT on a table in dvdrental
- Try dropping β observe error
- Fix and drop
β Task 3: Ownership Dependency
- Create role
manager - Assign database ownership
- Try dropping β observe error
- Fix using
REASSIGN OWNED
β Task 4: Table Ownership Test
ALTER TABLE film OWNER TO trainee;Try:
DROP ROLE trainee;π Fix it properly
π§ Key Concepts Summary
| Concept | Explanation |
|---|---|
| DROP ROLE | Deletes a user/role |
| Dependency | Role linked to objects |
| REASSIGN OWNED | Transfer ownership |
| DROP OWNED | Remove permissions |
| Ownership | Strongest control |
β οΈ Common Errors (Teach Students)
| Error | Reason |
|---|---|
| cannot drop role | still owns objects |
| permission denied | not superuser |
| role is in use | active connections |