Sub topic 5
GRANT and REVOKE in practice
This chapter assumes you know basic SQL, can connect to PostgreSQL with psql or another client, and understand that databases contain schemas and tables. The examples use group roles so that privileges are assigned to job functions rather than individual login accounts.
The access path
A privilege is permission to perform an action on a specific database object. GRANT adds permission. REVOKE removes a grant from a role or from PUBLIC.
A typical query must pass several checks:
A role commonly needs all of these permissions:
| Object | Privilege | Purpose |
|---|---|---|
| Database | `CONNECT` | Allows a session to connect |
| Schema | `USAGE` | Allows access to names and objects in the schema |
| Table | `SELECT` | Allows reading table rows |
| Table | `INSERT`, `UPDATE`, `DELETE` | Allows changing table rows |
Having USAGE on a schema does not grant access to its tables. Likewise, having SELECT on a table does not let a role connect to the database.
Granting privileges to group roles
Create non-login roles for access levels, then make a login role a member of the appropriate group:
CREATE ROLE app_readonly NOLOGIN;
CREATE ROLE app_writer NOLOGIN;
CREATE ROLE alice LOGIN PASSWORD 'change-me';
GRANT app_readonly TO alice;
GRANT CONNECT ON DATABASE appdb TO app_readonly, app_writer;
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;NOLOGIN roles are useful as permission containers. alice can connect, while the group roles describe what she can do.
You can grant several privileges in one statement. You can also grant access to every existing table in a schema:
GRANT SELECT ON ALL TABLES IN SCHEMA shop TO app_readonly;
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA shop TO app_writer;This command affects tables that exist when it runs. It does not automatically affect tables created later. Use ALTER DEFAULT PRIVILEGES for future objects, covered in a later chapter.
Revoking access
Use REVOKE with the same object and privilege style:
REVOKE UPDATE ON shop.orders FROM app_writer;
REVOKE SELECT ON shop.orders FROM alice;
REVOKE CONNECT ON DATABASE appdb FROM PUBLIC;The second statement removes a direct grant to alice. It does not remove SELECT that she receives through app_readonly, through PUBLIC, or through another membership. PostgreSQL calculates effective access from all applicable grants.
PUBLIC means every role, including roles created later. Check it before assuming that a revoke fully blocks access.
> callout note > Ownership is different from a grant. The owner can normally alter or drop the object and can grant its privileges to others. Revoking a privilege does not remove the owner's control.
Column-level privileges
For narrower access, name individual columns:
GRANT SELECT (id, customer) ON shop.orders TO alice;
GRANT UPDATE (customer) ON shop.orders TO app_writer;Column privileges can limit which columns a role reads or changes. They do not automatically solve every data-exposure problem. For row-based filtering, use row-level security.
Common errors
- Granting table access without schema
USAGE. - Forgetting database
CONNECT. - Assuming
ALL TABLESincludes future tables. - Revoking from a user while the user still inherits the privilege from a group role.
- Granting
ALLwhen the application needs only a few actions. - Forgetting that sequences and functions are separate object types with separate privileges.
Exercise
Alice belongs to app_readonly, which has SELECT on shop.orders. You run REVOKE SELECT ON shop.orders FROM alice. Can Alice still select from the table?
Show answer
Yes. The revoke removes only Alice's direct grant. She still receives SELECT through app_readonly unless that membership or group privilege is removed.