REVOKE Privileges in PostgreSQL | Remove Permissions from Roles
Learn how to use PostgreSQL REVOKE to remove privileges from roles and secure your database. Includes syntax, examples for SELECT, INSERT, UPDATE, DELETE, and REVOKE ALL.
๐ซ What is REVOKE in PostgreSQL?
In PostgreSQL, the REVOKE command is used to remove permissions (privileges) that were previously given using GRANT.
๐ In simple words: REVOKE = Take back access
๐ฏ Why do we use REVOKE?
We use REVOKE to:
- ๐ Remove user access for security
- ๐งโ๐ Restrict students/teachers when needed
- ๐ Stop unauthorized actions (INSERT, UPDATE, DELETE)
- ๐ Update role permissions in real systems
- ๐ซ Enforce rules in university or organization databases
๐งพ Basic Syntax
REVOKE privilege(s)
ON object_name
FROM role_name;๐งฉ Common Privileges to Revoke
| Privilege | Meaning |
|---|---|
| SELECT | Read data |
| INSERT | Add data |
| UPDATE | Modify data |
| DELETE | Remove data |
| ALL | Remove all privileges |
โ Simple Example
Step 1: Assume permission was given
GRANT SELECT ON students TO student;Step 2: Now remove it
REVOKE SELECT ON students FROM student;๐ Student can no longer read the table.
๐ซ Real-World University Example
๐ Scenario:
- Students were allowed to view marks
- Now exam period is over โ access must be restricted
โ Remove student access to marks
REVOKE SELECT ON marks FROM student;๐จโ๐ซ Restrict teachers from editing marks after submission
REVOKE INSERT, UPDATE ON marks FROM teacher;๐งโ๐ผ Remove all permissions from a role
REVOKE ALL PRIVILEGES ON students FROM it_staff;๐ REVOKE LAB: Remove Permissions Step-by-Step (University System)
๐ฏ Objective
You will learn how to:
- Remove access from users (roles)
- Restrict students, teachers, and staff
- Understand how security is tightened using
REVOKE
๐ PRE-SETUP (Assume Already Created)
We already have:
๐ Database:
university_db
๐ฅ Roles:
studentteacheradmin
๐ Tables:
studentscoursesmarksenrollments
๐ STEP 1: Check Existing Permissions (Important)
Before revoking, check what a role has:
\dp students๐ This shows current privileges on the table.
๐ซ STEP 2: Revoke Student Permissions (Full Restriction)
โ Remove SELECT access from students table
REVOKE SELECT ON students FROM student;โ Remove SELECT from all main tables
REVOKE SELECT ON courses FROM student;
REVOKE SELECT ON enrollments FROM student;
REVOKE SELECT ON marks FROM student;๐ Now student cannot read any data.
๐งช TEST AFTER REVOKE (Student Login)
SELECT * FROM students;โ ERROR: permission denied
๐จโ๐ซ STEP 3: Restrict Teacher Permissions
โ Remove ability to update marks
REVOKE UPDATE ON marks FROM teacher;โ Remove insert access
REVOKE INSERT ON marks FROM teacher;๐งช Test Teacher Access
INSERT INTO marks(student_id, course_id, marks)
VALUES (1, 1, 88);โ Should FAIL
๐งโ๐ผ STEP 4: Revoke ALL Permissions from Admin (Extreme Case)
โ ๏ธ Used in system reset or security emergency
REVOKE ALL PRIVILEGES ON students FROM admin;
REVOKE ALL PRIVILEGES ON courses FROM admin;
REVOKE ALL PRIVILEGES ON marks FROM admin;
REVOKE ALL PRIVILEGES ON enrollments FROM admin;๐ Admin loses table-level access (NOT superuser privilege if assigned separately)
๐ STEP 5: Revoke Database Access
โ Prevent login to database
REVOKE CONNECT ON DATABASE university_db FROM student;REVOKE CONNECT ON DATABASE university_db FROM teacher;๐งช STEP 6: Verify Connection Block
When student tries to connect:
\c university_dbโ ERROR: permission denied
๐ STEP 7: Advanced REVOKE (Cascade Effect)
If you granted multiple permissions:
REVOKE ALL ON marks FROM teacher;๐ Removes:
- SELECT
- INSERT
- UPDATE
- DELETE
๐ง STEP 8: Important Concepts
๐ GRANT vs REVOKE
| Command | Action |
|---|---|
| GRANT | Give permission |
| REVOKE | Remove permission |
โ ๏ธ Key Rules
โ You can only revoke what was granted โ Only owner or admin can revoke permissions โ REVOKE does NOT delete user โ REVOKE only removes access, not data
๐ซ REAL-WORLD UNIVERSITY USE CASE
Scenario:
University decides:
- Students should only view marks during exam week
- Teachers should stop editing marks after submission deadline
Solution:
REVOKE INSERT, UPDATE ON marks FROM teacher;๐ FINAL SUMMARY
After completing this lab, you learned:
โ How to remove permissions step-by-step โ How to restrict students, teachers, admins โ How database security is dynamically controlled โ Real-world control of university system access