Table-level permissions control who can see or change data in a whole table. This keeps data safe and organized.
Table-level permissions in PostgreSQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PostgreSQL
GRANT privilege_type ON table_name TO user_or_role; REVOKE privilege_type ON table_name FROM user_or_role;
privilege_type can be SELECT, INSERT, UPDATE, DELETE, or ALL PRIVILEGES.
You can grant permissions to a user or a group (role).
Examples
PostgreSQL
GRANT SELECT ON employees TO alice;
PostgreSQL
GRANT INSERT, UPDATE ON sales TO sales_team;
PostgreSQL
REVOKE DELETE ON customers FROM bob;
PostgreSQL
GRANT ALL PRIVILEGES ON orders TO manager;Sample Program
This creates a 'products' table and a user 'john'. Then it gives 'john' permission to read (SELECT) from the 'products' table only. The \dp command shows the permissions on the table.
PostgreSQL
CREATE TABLE products ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, price NUMERIC NOT NULL ); CREATE USER john; GRANT SELECT ON products TO john; -- Check permissions for john \dp products
Important Notes
Use GRANT ALL PRIVILEGES carefully; it gives full control.
Revoking permissions removes access immediately.
Permissions can be given to roles to manage groups of users easily.
Summary
Table-level permissions control who can read or change data in a table.
Use GRANT to give permissions and REVOKE to remove them.
Permissions help keep data safe and organized.
Practice
1. What does the
GRANT SELECT ON table_name TO user_name; command do in PostgreSQL?easy
Solution
Step 1: Understand the GRANT command
The GRANT command is used to give specific permissions to users on database objects like tables.Step 2: Identify the permission type SELECT
SELECT permission allows reading data from the table but not modifying it.Final Answer:
Allows the user to read data from the specified table. -> Option DQuick Check:
GRANT SELECT = read permission [OK]
Hint: GRANT SELECT means read access only [OK]
Common Mistakes:
- Confusing SELECT with DELETE permission
- Thinking GRANT creates tables
- Mixing GRANT with REVOKE commands
2. Which of the following is the correct syntax to revoke INSERT permission on a table named
employees from user john?easy
Solution
Step 1: Recall REVOKE syntax
The correct syntax is REVOKE permission ON table FROM user;Step 2: Match syntax with options
REVOKE INSERT ON employees FROM john; matches the correct order: REVOKE INSERT ON employees FROM john;Final Answer:
REVOKE INSERT ON employees FROM john; -> Option CQuick Check:
REVOKE permission ON table FROM user [OK]
Hint: REVOKE syntax: REVOKE permission ON table FROM user [OK]
Common Mistakes:
- Swapping ON and FROM keywords
- Using TO instead of FROM
- Incorrect order of clauses
3. Given the commands:
Which of the following is true about user permissions on the
GRANT SELECT ON orders TO alice; GRANT INSERT ON orders TO bob; REVOKE SELECT ON orders FROM alice;
Which of the following is true about user permissions on the
orders table?medium
Solution
Step 1: Analyze granted permissions
Alice was granted SELECT (read) permission, Bob was granted INSERT permission.Step 2: Analyze revoked permissions
Alice's SELECT permission was revoked, so she no longer can read data.Final Answer:
Alice cannot read data; Bob can insert data. -> Option AQuick Check:
Revoked SELECT removes read access [OK]
Hint: Revoking removes permission even if previously granted [OK]
Common Mistakes:
- Assuming revoked permission still applies
- Confusing INSERT with SELECT
- Thinking REVOKE affects other users
4. Consider this command:
What is the error in this command?
GRANT UPDATE ON customers TO ;
What is the error in this command?
medium
Solution
Step 1: Check syntax completeness
The command ends with TO but does not specify a user or role name.Step 2: Validate permission and table name
UPDATE is a valid permission and customers is the table name, so those parts are correct.Final Answer:
Missing user name after TO keyword. -> Option AQuick Check:
GRANT requires user after TO [OK]
Hint: Always specify user after TO in GRANT [OK]
Common Mistakes:
- Leaving user name blank after TO
- Confusing permission names
- Omitting table name
5. You want to allow user
carol to read and insert data into the products table but prevent her from deleting or updating any data. Which commands should you use?hard
Solution
Step 1: Grant only SELECT and INSERT permissions
To allow reading and inserting, grant SELECT and INSERT on products to carol.Step 2: Revoke DELETE and UPDATE permissions
To prevent deleting or updating, explicitly revoke DELETE and UPDATE permissions if previously granted.Final Answer:
GRANT SELECT, INSERT ON products TO carol; REVOKE DELETE, UPDATE ON products FROM carol; -> Option BQuick Check:
Grant needed permissions, revoke unwanted ones [OK]
Hint: Grant needed permissions, revoke unwanted explicitly [OK]
Common Mistakes:
- Granting ALL permissions instead of specific ones
- Not revoking unwanted permissions
- Granting DELETE or UPDATE by mistake
