Lesson 7of 8

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:

  • student
  • teacher
  • admin

๐Ÿ“‹ Tables:

  • students
  • courses
  • marks
  • enrollments

๐Ÿ” 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