Sub topic 9
Table and column privileges
This chapter assumes you know the basics of GRANT and REVOKE, that roles can be members of other roles, and the shop.orders table and the app_owner, app_readonly and app_writer roles from earlier chapters. Run the examples as a superuser or as app_owner.
Table privileges
Tables carry seven privileges that apply to rows and data. PostgreSQL 17 adds an eighth, MAINTAIN.
| Privilege | What it allows | Gotcha |
|---|---|---|
| `SELECT` | Read rows with `SELECT`, `COPY TO`, `RETURNING` | Also required by `UPDATE` and `DELETE` that read columns |
| `INSERT` | Add rows | `INSERT ... RETURNING` also needs `SELECT` on the returned columns |
| `UPDATE` | Change rows | Needs `SELECT` as well if a `WHERE` clause or expression reads a column |
| `DELETE` | Remove rows | Needs `SELECT` if the `WHERE` clause reads columns |
| `TRUNCATE` | Empty the table with `TRUNCATE` | Separate from `DELETE` |
| `REFERENCES` | Create a foreign key that points at the table | Needed on the referenced table, not the referencing one |
| `TRIGGER` | Create triggers on the table |
GRANT ALL on a table grants every privilege in that list. Ownership is separate. Only the owner (or a member of the owning role) can ALTER or DROP the table. No grantable privilege covers that.
The SELECT dependency catches people. A writer with only UPDATE can run UPDATE shop.orders SET customer = 'x', but UPDATE ... WHERE id = 1 fails, because the WHERE clause reads id. For the same reason, INSERT ... ON CONFLICT DO UPDATE needs INSERT, UPDATE, and SELECT when the DO UPDATE part reads existing values.
The grants from the earlier chapter follow this rule:
GRANT SELECT ON ALL TABLES IN SCHEMA shop TO app_readonly;
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA shop TO app_writer;The writer role gets SELECT along with INSERT and UPDATE, and nobody gets DELETE. ALL TABLES IN SCHEMA covers only tables that exist when the command runs. Tables created later need ALTER DEFAULT PRIVILEGES, which a later chapter covers.
Column privileges
Four privileges can be granted on individual columns: SELECT, INSERT, UPDATE, and REFERENCES. The column list goes in parentheses after the privilege name.
GRANT SELECT (id, customer) ON shop.orders TO alice;
GRANT UPDATE (customer) ON shop.orders TO app_writer;The two rules below decide how column privileges behave:
- A table-level privilege covers every column, including columns added later. A column-level grant covers only the named columns. A column you add afterwards is not covered.
- Privileges are additive. If a role holds a privilege at the table level, directly or through an inherited membership, a column-level grant for the same privilege adds nothing, and a column-level
REVOKEtakes nothing away.
Both statements above are redundant in the setup from earlier chapters. alice inherits table-level SELECT from app_readonly, and app_writer already has table-level UPDATE. To make column grants take effect, the role must have no table-level grant for that privilege.
A worked example
This example creates a support role that can read order IDs and customer names, and can change only the customer name.
CREATE ROLE support_agent LOGIN PASSWORD 'change-me';
GRANT USAGE ON SCHEMA shop TO support_agent;
GRANT SELECT (id, customer) ON shop.orders TO support_agent;
GRANT UPDATE (customer) ON shop.orders TO support_agent;
SET ROLE support_agent;
SELECT id, customer FROM shop.orders; -- works
SELECT total FROM shop.orders; -- permission denied
SELECT * FROM shop.orders; -- permission denied
UPDATE shop.orders SET customer = 'Acme' WHERE id = 1; -- works
UPDATE shop.orders SET customer = 'Acme'
WHERE total > 100; -- permission denied
UPDATE shop.orders SET total = 0 WHERE id = 1; -- permission denied
RESET ROLE;Three points about this session:
SELECT *fails because it expands to every column, andsupport_agentlackstotalandsales_rep. Applications that useSELECT *break when you move them to column grants. Name the columns explicitly.- The error reads
permission denied for table orders. It names the table, not the column, so you have to work out which column caused it. - The first
UPDATEworks because itsWHEREclause reads onlyid, whichsupport_agentcanSELECT. TheWHERE total > 100version reads a column the role cannot see.
A query that references no columns, such as SELECT count(*) FROM shop.orders, needs SELECT on at least one column of the table.
Column-level INSERT
A role with column-level INSERT can name only the permitted columns. Every column it omits receives its default value, or NULL if there is no default.
GRANT INSERT (customer, sales_rep, total) ON shop.orders TO support_agent;The id column is GENERATED ALWAYS AS IDENTITY, so the role does not supply it and the database generates it. If a column has no default and is NOT NULL, a role without INSERT on it can never insert a valid row. Check this before you restrict INSERT.
Revoking, and the order that matters
A table-level REVOKE also removes the column-level privileges on that table. That makes it the safe way to convert a role from table-level to column-level access:
REVOKE SELECT ON shop.orders FROM support_agent; -- also clears column grants
GRANT SELECT (id, customer) ON shop.orders TO support_agent;Do the revoke first. If you grant columns and then revoke the table-level privilege, the column grants go too.
The reverse does not work. REVOKE SELECT (total) ON shop.orders FROM alice removes only a column-level grant made directly to alice. It has no effect on SELECT that alice inherits from app_readonly. To hide total from a role that reads through a group, you have three options:
- Take the table-level
SELECTaway from the group. - Grant the visible columns to the group instead.
- Move the user into a different group that already has column grants.
Checking column privileges
Several tools report column-level grants:
SELECT has_column_privilege('support_agent', 'shop.orders', 'total', 'SELECT')
AS can_read_total;
SELECT has_any_column_privilege('support_agent', 'shop.orders', 'SELECT')
AS can_read_something;
SELECT has_table_privilege('support_agent', 'shop.orders', 'SELECT')
AS can_read_whole_table;
SELECT grantee, column_name, privilege_type
FROM information_schema.column_privileges
WHERE table_schema = 'shop' AND table_name = 'orders'
ORDER BY grantee, column_name, privilege_type;For support_agent these return false, true, and false. has_column_privilege and has_any_column_privilege count privileges inherited from table-level grants. has_table_privilege is false when only column grants exist. In psql, \dp shop.orders shows table privileges in the Access privileges column and column privileges in a separate Column privileges column.
information_schema.column_privileges lists only grants that the current user can see through ownership or membership. Use pg_attribute.attacl or the has_* functions when you audit as a superuser.
When column grants are the wrong tool
Column privileges suit a small number of fixed, sensitive columns, such as salary, national ID, or card data. They have costs:
- Every new column is invisible to restricted roles until you grant it, which is the safe default.
SELECT *and ORMs that generate full column lists stop working.- Per-table, per-column grants are tedious to audit across many tables.
Two alternatives are worth knowing. A view that selects only the permitted columns, owned by a privileged role, gives restricted roles a stable surface and lets you rename or compute columns. Row-level security, covered in a later chapter, filters rows instead of columns. Use column grants when the rule is "this role never sees this field". Use views or row-level security when the rule depends on the row.
Exercises
Exercise
alice is a member of app_readonly, which holds table-level SELECT on shop.orders, and she inherits its privileges. You run REVOKE SELECT (total) ON shop.orders FROM alice. Can alice still read total? How would you hide total from her while still letting her read id and customer?
Show answer
Yes, she can still read total. The REVOKE removes only a column-level grant made directly to alice, and she has none. She still inherits table-level SELECT through appreadonly. To hide total, take the table-level SELECT away from the role she reads through, for example REVOKE SELECT ON shop.orders FROM appreadonly, and then grant SELECT (id, customer) ON shop.orders to app_readonly or to alice.
Exercise
supportagent holds SELECT (id, customer) and UPDATE (customer) on shop.orders. Which of these succeed: (a) UPDATE shop.orders SET customer = 'X' WHERE id = 7; (b) UPDATE shop.orders SET customer = 'X' WHERE salesrep = 'bob'; (c) SELECT count(*) FROM shop.orders;
Show answer
(a) and (c) succeed. (a) writes customer, which UPDATE (customer) allows, and reads only id in the WHERE clause, which SELECT (id, ...) allows. (b) fails because the WHERE clause reads sales_rep, and the role has no SELECT on that column. (c) succeeds because count(*) references no columns and the role holds SELECT on at least one column.