Learn with Yasir

Share Your Feedback

GRANT Privileges in PostgreSQL | Grant Permissions to Roles & Tables


Learn how to use PostgreSQL GRANT to give SELECT, INSERT, UPDATE, DELETE and CONNECT privileges to roles. Includes syntax, common privileges, and practical examples.

๐Ÿ“Œ What is GRANT in PostgreSQL?

In PostgreSQL, the GRANT command is used to give permissions (privileges) to users (roles) on database objects like tables, schemas, or databases.

๐Ÿ‘‰ In simple words: GRANT = Give access to someone


๐ŸŽฏ Why do we use GRANT?

We use GRANT to:

  • ๐Ÿ” Control who can access what
  • ๐Ÿ‘จโ€๐Ÿ’ป Allow users to perform actions like:

    • SELECT (read data)
    • INSERT (add data)
    • UPDATE (modify data)
    • DELETE (remove data)
  • ๐Ÿ›ก๏ธ Improve database security
  • ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘ Support multi-user environments

๐Ÿงพ Basic Syntax

GRANT privilege(s)
ON object_name
TO role_name;

๐Ÿงฉ Common Privileges

Privilege Meaning
SELECT Read data
INSERT Add new rows
UPDATE Modify existing data
DELETE Remove rows
ALL All privileges

โœ… Example

Step 1: Create a user (role)

CREATE ROLE trainee WITH LOGIN PASSWORD '1234';

Step 2: Grant access to a table

GRANT SELECT ON customer TO trainee;

๐Ÿ‘‰ Now trainee can only view data from customer table.


๐Ÿ”„ Multiple Privileges Example

GRANT SELECT, INSERT, UPDATE
ON customer
TO trainee;

๐Ÿ‘‰ Now user can:

  • Read
  • Add
  • Modify data

๐ŸŒ Grant on Entire Database

GRANT CONNECT ON DATABASE dvdrental TO trainee;

๐Ÿ‘‰ Allows user to connect to the dvdrental database.


๐Ÿง  Important Notes

  • You must be owner or superuser to grant permissions.
  • Permissions can be revoked using REVOKE.
  • Roles can be users or groups.

๐Ÿงช Practical Lab Exercise (Beginner Friendly)

๐ŸŽฏ Objective:

Learn how to control access using GRANT.


๐Ÿ”น Task 1: Create a Role

CREATE ROLE student1 WITH LOGIN PASSWORD 'pass123';

๐Ÿ”น Task 2: Grant Database Access

GRANT CONNECT ON DATABASE dvdrental TO student1;

๐Ÿ”น Task 3: Grant Read Access

GRANT SELECT ON film TO student1;

๐Ÿ‘‰ Test: Login as student1 and run:

SELECT * FROM film;

๐Ÿ”น Task 4: Try Restricted Action

INSERT INTO film(title) VALUES ('Test Movie');

โŒ This should fail (no INSERT permission)


๐Ÿ”น Task 5: Grant INSERT Permission

GRANT INSERT ON film TO student1;

๐Ÿ‘‰ Try INSERT again โ†’ โœ… Should work


๐Ÿ”น Task 6: Grant All Permissions

GRANT ALL ON film TO student1;

๐Ÿ”น Challenge Task ๐Ÿ’ก

  1. Create another role viewer
  2. Allow only SELECT on actor table
  3. Block all other operations

๐Ÿงพ Summary

  • GRANT = Give permission
  • Used for security & access control
  • Works on tables, databases, schemas
  • Helps manage multiple users safely

Below is a complete step-by-step real-world university system scenario showing:

โœ” Database creation โœ” Table design โœ” Role creation โœ” GRANT usage (security layer) โœ” Testing access like a real system

Using PostgreSQL


๐ŸŽ“ University System with GRANT (Step-by-Step Lab)

๐Ÿ Goal

We will build a small University Database System where:

  • Students can only view data
  • Teachers can manage marks
  • Admin controls everything using GRANT

ERD Diagram

ERD Diagram for University Database System


๐Ÿงฑ STEP 1: Create Database

CREATE DATABASE university_db;

๐Ÿ‘‰ Now connect to database:

\c university_db

๐Ÿ—๏ธ STEP 2: Create Main Tables

๐Ÿ“˜ 2.1 Students Table

CREATE TABLE students (
    student_id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    department VARCHAR(50),
    email VARCHAR(100)
);

๐Ÿ“˜ 2.2 Courses Table

CREATE TABLE courses (
    course_id SERIAL PRIMARY KEY,
    course_name VARCHAR(100),
    credit_hours INT
);

๐Ÿ“˜ 2.3 Enrollments Table

CREATE TABLE enrollments (
    enroll_id SERIAL PRIMARY KEY,
    student_id INT REFERENCES students(student_id),
    course_id INT REFERENCES courses(course_id)
);

๐Ÿ“˜ 2.4 Marks Table

CREATE TABLE marks (
    mark_id SERIAL PRIMARY KEY,
    student_id INT REFERENCES students(student_id),
    course_id INT REFERENCES courses(course_id),
    marks INT
);

๐Ÿ‘ฅ STEP 3: Create Roles (Users)

๐ŸŽ“ Student Role

CREATE ROLE student WITH LOGIN PASSWORD '123';

๐Ÿ‘จโ€๐Ÿซ Teacher Role

CREATE ROLE teacher WITH LOGIN PASSWORD '123';

๐Ÿง‘โ€๐Ÿ’ผ Admin Role

CREATE ROLE admin WITH LOGIN PASSWORD 'admin123';

๐Ÿ” STEP 4: Grant Database Access

Allow connection to database

GRANT CONNECT ON DATABASE university_db TO student;
GRANT CONNECT ON DATABASE university_db TO teacher;
GRANT CONNECT ON DATABASE university_db TO admin;

๐Ÿ“Š STEP 5: GRANT Permissions (Core Security)


๐ŸŽ“ 5.1 Student Permissions (Read Only)

GRANT SELECT ON students TO student;
GRANT SELECT ON courses TO student;
GRANT SELECT ON enrollments TO student;
GRANT SELECT ON marks TO student;

๐Ÿ‘‰ Students can ONLY VIEW data

โŒ Cannot insert/update/delete


๐Ÿ‘จโ€๐Ÿซ 5.2 Teacher Permissions

GRANT SELECT ON students TO teacher;
GRANT SELECT ON courses TO teacher;
GRANT SELECT ON enrollments TO teacher;

Teachers can manage marks:

GRANT SELECT, INSERT, UPDATE ON marks TO teacher;

๐Ÿง‘โ€๐Ÿ’ผ 5.3 Admin Permissions (Full Control)

GRANT ALL PRIVILEGES ON students TO admin;
GRANT ALL PRIVILEGES ON courses TO admin;
GRANT ALL PRIVILEGES ON enrollments TO admin;
GRANT ALL PRIVILEGES ON marks TO admin;

๐Ÿงช STEP 6: Insert Sample Data (Admin Role)

INSERT INTO students (name, department, email)
VALUES 
('Ali Khan', 'CS', 'ali@uni.edu'),
('Sara Ahmed', 'IT', 'sara@uni.edu');
INSERT INTO courses (course_name, credit_hours)
VALUES 
('Database Systems', 3),
('Web Development', 4);

๐Ÿ‘จโ€๐ŸŽ“ STEP 7: Testing Role Access


๐Ÿ” Login as Student

SELECT * FROM students;

โœ” Allowed


โŒ Try Insert (Should FAIL)

INSERT INTO students(name, department, email)
VALUES ('Test Student', 'CS', 'test@uni.edu');

๐Ÿ‘‰ ERROR: permission denied


๐Ÿ‘จโ€๐Ÿซ Login as Teacher

โœ” View students

SELECT * FROM students;

โœ” Insert marks

INSERT INTO marks(student_id, course_id, marks)
VALUES (1, 1, 85);

โœ” Update marks

UPDATE marks
SET marks = 90
WHERE student_id = 1;

๐Ÿง‘โ€๐Ÿ’ผ Login as Admin

Full control:

DELETE FROM students WHERE student_id = 2;

โœ” Works


๐Ÿง  STEP 8: How GRANT Works (Concept)

Think of it like a University Gate System:

Role Access Level
Student Read only (library access)
Teacher Edit marks + view data
Admin Full control (system owner)

๐Ÿ” STEP 9: Security Logic (Important)

โœ” GRANT = give permission โœ” REVOKE = remove permission โœ” Roles = users or groups

๐Ÿ‘‰ Without GRANT: Users cannot even see tables


๐Ÿ“Œ FINAL SUMMARY

In this real-world university system:

โœ” Database created โœ” Tables designed โœ” Roles created โœ” Permissions assigned using GRANT โœ” Access controlled by role type