Column-level permissions let you control who can see or change specific columns in a table. This keeps sensitive data safe.
Column-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 SELECT (column_name) ON table_name TO role_name; REVOKE SELECT (column_name) ON table_name FROM role_name;
You specify the columns inside parentheses after the privilege type.
You can grant or revoke permissions on specific columns separately from the whole table.
Examples
PostgreSQL
GRANT SELECT (name, email) ON customers TO sales_team;
PostgreSQL
REVOKE SELECT (salary) ON employees FROM public;
PostgreSQL
GRANT UPDATE (phone) ON customers TO support_team;
Sample Program
This example creates an employees table and a role hr_team. It grants hr_team permission to see only the name and email columns, not salary. Then it switches to hr_team role and tries to select all columns.
PostgreSQL
CREATE TABLE employees ( id SERIAL PRIMARY KEY, name TEXT, email TEXT, salary NUMERIC ); INSERT INTO employees (name, email, salary) VALUES ('Alice', 'alice@example.com', 70000), ('Bob', 'bob@example.com', 60000); CREATE ROLE hr_team; GRANT SELECT (name, email) ON employees TO hr_team; REVOKE SELECT (salary) ON employees FROM hr_team; SET ROLE hr_team; SELECT * FROM employees;
Important Notes
If a role does not have permission on a column, that column will show as NULL when selected.
Column-level permissions work together with table-level permissions. You need SELECT on the table or on the columns.
Summary
Column-level permissions control access to specific columns in a table.
Use GRANT and REVOKE with column names in parentheses.
Restricted columns show as NULL if the user lacks permission.
Practice
1. What does column-level permission in PostgreSQL control?
easy
Solution
Step 1: Understand the concept of column-level permissions
Column-level permissions allow control over which columns a user can see or modify in a table.Step 2: Compare with other access types
Other options refer to broader or unrelated access controls, not specific columns.Final Answer:
Access to specific columns in a table -> Option DQuick Check:
Column-level permission = Access to specific columns [OK]
Hint: Column-level means controlling access per column, not whole table [OK]
Common Mistakes:
- Confusing column-level with table-level permissions
- Thinking it controls schema or server access
- Assuming it controls row-level access
2. Which of the following is the correct syntax to grant SELECT permission on column
email of table users to user alice?easy
Solution
Step 1: Recall PostgreSQL syntax for column-level GRANT
The correct syntax is GRANT SELECT (email) ON users TO alice; (parentheses after privilege).Step 2: Match options with syntax
GRANT SELECT(email) ON users TO alice; matches the correct syntax exactly, others misplace keywords or parentheses.Final Answer:
GRANT SELECT(email) ON users TO alice; -> Option AQuick Check:
GRANT SELECT(column) ON table TO user [OK]
Hint: GRANT SELECT(column) ON table TO user is correct syntax [OK]
Common Mistakes:
- Placing column name after TO user
- Putting column inside ON table()
- Misordering keywords in the statement
3. Given the table
employees(id, name, salary), if user bob has SELECT permission only on id and name, what will be the result of SELECT * FROM employees; executed by bob?medium
Solution
Step 1: Understand column-level permission effect on SELECT *
User can see columns they have permission for; restricted columns appear as NULL.Step 2: Apply to given columns
Bob has permission onidandname, sosalaryshows as NULL.Final Answer:
Only id and name columns with data, salary as NULL -> Option AQuick Check:
Restricted columns show NULL, not error [OK]
Hint: Restricted columns appear as NULL, not error [OK]
Common Mistakes:
- Expecting a permission denied error
- Assuming all columns show data
- Thinking restricted columns are hidden completely
4. You run the command
GRANT SELECT ON employees(name, salary) TO carol; but get a syntax error. What is the likely cause?medium
Solution
Step 1: Check syntax for GRANT with multiple columns
Correct syntax is GRANT SELECT (name, salary) ON employees TO carol;Step 2: Identify error in command
Command incorrectly places column list after table name; column list must follow SELECT before ON.Final Answer:
Column list in parentheses must precede the table name -> Option CQuick Check:
GRANT SELECT (col1, col2) ON table TO user [OK]
Hint: Columns inside parentheses after SELECT before ON table [OK]
Common Mistakes:
- Placing column list after table name
- Forgetting parentheses around columns
- Assuming multiple columns need separate GRANTs
5. You want to allow user
dave to update only the phone column in the contacts table but not others. Which sequence of commands correctly achieves this?hard
Solution
Step 1: Remove any existing full UPDATE permission
First revoke any broad UPDATE permission to avoid conflicts.Step 2: Grant UPDATE permission only on the phone column
Then grant UPDATE on the specific column to limit access.Final Answer:
REVOKE UPDATE ON contacts FROM dave; GRANT UPDATE(phone) ON contacts TO dave; -> Option BQuick Check:
Revoke broad then grant column-level UPDATE [OK]
Hint: Revoke broad permission before granting column-level update [OK]
Common Mistakes:
- Granting column-level without revoking broad permission
- Revoking column-level instead of broad permission
- Assuming single GRANT is enough if broad permission exists
