Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Managing User Permissions with GRANT and REVOKE in PostgreSQL
📖 Scenario: You are a database administrator for a small company. You need to control who can access and modify the employees table in your PostgreSQL database. You will practice giving and taking away permissions to users.
🎯 Goal: Learn how to use GRANT to give permissions and REVOKE to remove permissions on a table in PostgreSQL.
📋 What You'll Learn
Create a table named employees with columns id (integer) and name (text).
Grant SELECT permission on employees to a user named alice.
Revoke SELECT permission on employees from the user alice.
Grant INSERT permission on employees to a user named bob.
💡 Why This Matters
🌍 Real World
Database administrators often need to control who can read or change data. Using GRANT and REVOKE helps keep data safe and organized.
💼 Career
Knowing how to manage permissions is essential for roles like database administrator, backend developer, and data engineer.
Progress0 / 4 steps
1
Create the employees table
Write a SQL statement to create a table called employees with two columns: id as an integer and name as text.
PostgreSQL
Hint
Use CREATE TABLE employees (id INTEGER, name TEXT); to create the table.
2
Grant SELECT permission to alice
Write a SQL statement to grant SELECT permission on the employees table to the user alice.
PostgreSQL
Hint
Use GRANT SELECT ON employees TO alice; to give read access.
3
Revoke SELECT permission from alice
Write a SQL statement to revoke SELECT permission on the employees table from the user alice.
PostgreSQL
Hint
Use REVOKE SELECT ON employees FROM alice; to remove read access.
4
Grant INSERT permission to bob
Write a SQL statement to grant INSERT permission on the employees table to the user bob.
PostgreSQL
Hint
Use GRANT INSERT ON employees TO bob; to allow adding new rows.
Practice
(1/5)
1. What does the GRANT command do in PostgreSQL?
easy
A. It gives specific permissions to a user or role.
B. It deletes a user from the database.
C. It creates a new database.
D. It backs up the database.
Solution
Step 1: Understand the purpose of GRANT
The GRANT command is used to give permissions like SELECT, INSERT, or UPDATE to users or roles.
Step 2: Compare with other options
Deleting users, creating databases, or backing up are done by other commands, not GRANT.
Final Answer:
It gives specific permissions to a user or role. -> Option A
Quick Check:
GRANT = give permissions [OK]
Hint: GRANT means give rights; REVOKE means take away [OK]
Common Mistakes:
Confusing GRANT with user creation
Thinking GRANT deletes data
Mixing GRANT with backup commands
2. Which of the following is the correct syntax to grant SELECT permission on table employees to user john?
easy
A. GRANT ON employees SELECT TO john;
B. GRANT john ON employees SELECT;
C. GRANT SELECT TO john ON employees;
D. GRANT SELECT ON employees TO john;
Solution
Step 1: Recall correct GRANT syntax
The correct syntax is: GRANT permission ON object TO user;
Step 2: Match syntax with options
GRANT SELECT ON employees TO john; matches the correct order: GRANT SELECT ON employees TO john;
Final Answer:
GRANT SELECT ON employees TO john; -> Option D
Quick Check:
GRANT + permission + ON + object + TO + user [OK]
Hint: GRANT permission ON object TO user; is the pattern [OK]
Common Mistakes:
Mixing order of keywords
Placing user before permission
Omitting ON keyword
3. Given the commands: GRANT SELECT ON customers TO alice; REVOKE SELECT ON customers FROM alice; What permissions does user alice have on table customers after these commands?
medium
A. Alice can SELECT from customers.
B. Alice can INSERT into customers.
C. Alice cannot SELECT from customers.
D. Alice has all permissions on customers.
Solution
Step 1: Analyze the GRANT command
Alice is given SELECT permission on customers table.
Step 2: Analyze the REVOKE command
The SELECT permission is then revoked from Alice, removing her ability to SELECT.
4. Identify the error in the following command: REVOKE ALL PRIVILEGES employees FROM bob;
medium
A. The order of keywords is incorrect.
B. The user name should be before the table name.
C. REVOKE cannot be used with ALL PRIVILEGES.
D. The command is correct.
Solution
Step 1: Check correct REVOKE syntax
The correct syntax is: REVOKE privileges ON object FROM user;
Step 2: Compare with given command
The command uses: REVOKE ALL PRIVILEGES employees FROM bob; missing ON keyword and wrong order.
Final Answer:
The order of keywords is incorrect. -> Option A
Quick Check:
REVOKE + privileges + ON + object + FROM + user [OK]
Hint: REVOKE syntax requires ON before object [OK]
Common Mistakes:
Omitting ON keyword
Swapping user and object positions
Using ALL PRIVILEGES incorrectly
5. You want to allow user carol to insert and update data on table orders, but not delete. Which commands correctly grant these permissions?
hard
A. GRANT DELETE ON orders TO carol;
B. GRANT INSERT, UPDATE ON orders TO carol;
C. GRANT ALL PRIVILEGES ON orders TO carol;
D. GRANT SELECT, DELETE ON orders TO carol;
Solution
Step 1: Identify required permissions
Carol needs INSERT and UPDATE permissions only, no DELETE.
Step 2: Match commands with required permissions
GRANT INSERT, UPDATE ON orders TO carol; grants INSERT and UPDATE correctly. Options B and D grant DELETE, which is not wanted. GRANT ALL PRIVILEGES ON orders TO carol; grants all permissions, including DELETE.
Final Answer:
GRANT INSERT, UPDATE ON orders TO carol; -> Option B
Quick Check:
Grant only needed permissions, avoid ALL PRIVILEGES if not required [OK]
Hint: Grant only needed permissions, avoid ALL PRIVILEGES if unsure [OK]