Sub topic 5
GRANT and REVOKE in practice
This chapter assumes you know how roles and object ownership work, as covered in the earlier chapters. You should also be comfortable running SQL in psql as a superuser or a role with enough rights to create roles. The examples use the shop schema from this tutorial.
The shape of the commands
Both commands name a privilege list, an object, and one or more roles.
GRANT privilege [, ...] ON object_type object_name TO role [, ...];
REVOKE privilege [, ...] ON object_type object_name FROM role [, ...];A grant is stored as an entry in the object's access control list (ACL). Changes apply to the next statement a session runs, so users do not need to reconnect. Both commands run inside transactions and roll back with them.
Start with a self-contained setup. Run it as a superuser.
CREATE ROLE app_owner NOLOGIN;
CREATE ROLE app_readonly NOLOGIN;
CREATE ROLE app_writer NOLOGIN;
CREATE ROLE team_lead NOLOGIN;
CREATE ROLE alice LOGIN PASSWORD 'change-me';
CREATE SCHEMA shop AUTHORIZATION app_owner;
SET ROLE app_owner;
CREATE TABLE shop.orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer text NOT NULL,
sales_rep name NOT NULL,
total numeric(10,2) NOT NULL
);
RESET ROLE;Granting and reading the result
Reaching a table takes two grants: USAGE on the schema and a privilege on the table. Without the first, the second does nothing.
GRANT USAGE ON SCHEMA shop TO app_readonly, app_writer;
GRANT SELECT ON shop.orders TO app_readonly;
GRANT SELECT, INSERT, UPDATE ON shop.orders TO app_writer;In psql, \dp shop.orders shows the result in compact ACL notation:
app_owner=arwdDxt/app_owner
app_readonly=r/app_owner
app_writer=arw/app_ownerEach entry reads grantee=privileges/grantor. The letters are abbreviations:
| Letter | Privilege |
|---|---|
| r | SELECT |
| a | INSERT |
| w | UPDATE |
| d | DELETE |
| D | TRUNCATE |
| x | REFERENCES |
| t | TRIGGER |
The owner's own entry may contain extra letters in newer versions. PostgreSQL 17 adds m for MAINTAIN, for example.
Granting the same privilege twice is harmless. GRANT is idempotent, so setup scripts can be rerun.
Granting on many objects at once
ALL TABLES IN SCHEMA expands to the tables, views, and foreign tables that exist when the command runs.
GRANT SELECT ON ALL TABLES IN SCHEMA shop TO app_readonly;A table created tomorrow is not covered. A later chapter covers ALTER DEFAULT PRIVILEGES for that case. The command also changes nothing about schema access, so USAGE on the schema is still required.
Who is allowed to grant
You can grant a privilege on an object only if one of these is true:
- You are a superuser.
- You own the object, or you are a member of the owning role in a way that lets you act as it.
- You hold the privilege with
GRANT OPTION.
A privilege granted by a superuser is recorded as granted by the object's owner. That keeps the ACL consistent no matter which administrator ran the command.
If you hold a privilege on an object but without grant option, GRANT may not fail. It can print WARNING: no privileges were granted for "orders" and carry on. Check the output of grant scripts for warnings instead of relying on the exit status alone. If you hold no privilege at all, you get permission denied instead.
Delegating with WITH GRANT OPTION
WITH GRANT OPTION lets the grantee pass the privilege on.
GRANT SELECT ON shop.orders TO team_lead WITH GRANT OPTION;
SET ROLE team_lead;
GRANT SELECT ON shop.orders TO alice;
RESET ROLE;PostgreSQL now tracks a chain: app_owner granted to team_lead, and team_lead granted to alice.
Revoking, and why it sometimes refuses
REVOKE mirrors GRANT.
REVOKE UPDATE ON shop.orders FROM app_writer;Dependent grants block a plain revoke. Try to remove team_lead's privilege while alice depends on it:
REVOKE SELECT ON shop.orders FROM team_lead;
-- ERROR: dependent privileges exist
-- HINT: Use CASCADE to revoke them too.You have two choices:
-- Remove the privilege and everything granted through it
REVOKE SELECT ON shop.orders FROM team_lead CASCADE;
-- Or keep team_lead's own SELECT, and remove only the right to pass it on
REVOKE GRANT OPTION FOR SELECT ON shop.orders FROM team_lead CASCADE;The second form still cascades. It removes alice's grant, because that grant came through the option you just took away. RESTRICT is the default and gives the error shown above.
Revoke only undoes your own grants
A REVOKE removes only the grants made by the role running it (or the role it acts as). If both app_owner and team_lead granted SELECT to alice, revoking as app_owner leaves team_lead's grant in place, and alice keeps access. When a revoke seems to have no effect, run \dp and look at the grantor after the slash.
Table-level and column-level grants interact
A table-level privilege covers every column. Revoking a single column does not carve a hole in it.
GRANT SELECT ON shop.orders TO app_readonly;
REVOKE SELECT (total) ON shop.orders FROM app_readonly;
-- No effect: the table-level SELECT still covers total.Verifying the outcome
Never assume a grant worked. Ask the server:
SELECT has_table_privilege('alice', 'shop.orders', 'SELECT') AS can_select;
SELECT has_schema_privilege('alice', 'shop', 'USAGE') AS can_use_schema;has_table_privilege answers for the role's effective rights, including inherited ones. The \dp command shows only entries stored on the object. A role that gets access through membership in app_readonly returns true from the function, but does not appear in \dp.
Common errors
| Message | Likely cause | Fix |
|---|---|---|
| `permission denied for schema shop` | Schema `USAGE` is missing | `GRANT USAGE ON SCHEMA shop TO role` |
| `permission denied for table orders` | Table privilege is missing, or `SELECT` is missing for an `UPDATE ... WHERE` | Grant the needed privileges |
| `WARNING: no privileges were granted` | You hold the privilege without grant option | Ask the owner for `WITH GRANT OPTION`, or have the owner run the grant |
| `dependent privileges exist` | A grantee passed the privilege on | Use `CASCADE`, or revoke `GRANT OPTION FOR` |
| Revoke runs but access remains | Another grantor, a group role, or `PUBLIC` also grants it | Check `\dp` and membership |
An UPDATE or DELETE with a WHERE clause that reads columns also needs SELECT on those columns. That is why app_writer above holds SELECT alongside UPDATE.
Exercise
You ran REVOKE SELECT ON shop.orders FROM alice; as the table owner, yet has_table_privilege('alice', 'shop.orders', 'SELECT') still returns true. Name two causes and a query or command that identifies each.
Show answer
First, alice may be a member of a role such as appreadonly that holds SELECT and that she inherits. Check with \du alice or `pgauthmembers, and \dp shop.orders to see which roles have entries. Second, SELECT may be granted to PUBLIC, which appears as an entry with an empty grantee (=r/appowner) in \dp. A third possibility is a grant by another grantor, which the owner's revoke does not remove. The grantor shows after the slash in \dp`.
Exercise
Write the statements that let team_lead grant INSERT on shop.orders to others, then remove team_lead's ability to delegate while leaving team_lead's own INSERT in place. Explain what happens to roles that received INSERT from team_lead.
Show answer
GRANT INSERT ON shop.orders TO team_lead WITH GRANT OPTION; then REVOKE GRANT OPTION FOR INSERT ON shop.orders FROM team_lead CASCADE;. The CASCADE keyword is required if anyone received INSERT through teamlead. Those downstream grants are removed, because they depended on the grant option. teamlead keeps plain INSERT.