Sub topic 4
The Privilege Types and Which Objects They Apply To
Assume you can connect to PostgreSQL, create tables, and run basic SELECT, INSERT, UPDATE, and DELETE statements. This chapter uses the terms database, schema, table, sequence, and function in their PostgreSQL sense.
A privilege is permission to perform an operation on a database object. A role can receive privileges directly, through membership in another role, or through the special PUBLIC role. Ownership is different: the owner controls the object and can usually grant or revoke its privileges.
Match privileges to object types
Each object supports its own privilege set. The common mappings are:
| Object | Important privileges | What they allow |
|---|---|---|
| Database | `CONNECT`, `CREATE`, `TEMPORARY` | Connect, create schemas, create temporary tables |
| Schema | `USAGE`, `CREATE` | Resolve objects in the schema, create objects there |
| Table | `SELECT`, `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, `REFERENCES`, `TRIGGER` | Read, add, change, remove, empty, reference, or trigger table data |
| Sequence | `USAGE`, `SELECT`, `UPDATE` | Use sequence values, inspect values, or change sequence state |
| Function | `EXECUTE` | Call the function |
USAGE on a schema does not grant access to tables inside it. A role commonly needs both schema USAGE and table privileges. Similarly, USAGE on a sequence does not grant INSERT on a table that uses that sequence.
A table's UPDATE privilege can apply to selected columns. SELECT, INSERT, and REFERENCES can also be limited to named columns. TRUNCATE is separate from DELETE: deleting rows can be restricted by row-level security, while truncating the table is a distinct operation.
Grant privileges explicitly
The basic form is:
GRANT privilege_name
ON object_type object_name
TO role_name;This example gives a reporting role access to a table but not to the table's schema:
CREATE ROLE reporting NOLOGIN;
CREATE SCHEMA shop;
CREATE TABLE shop.orders (
id bigint PRIMARY KEY,
customer text NOT NULL,
total numeric(10, 2) NOT NULL
);
GRANT SELECT ON shop.orders TO reporting;
GRANT USAGE ON SCHEMA shop TO reporting;The second GRANT is required when the role accesses the table through the shop schema. You can grant several privileges in one command:
GRANT SELECT, INSERT, UPDATE ON shop.orders TO reporting;
GRANT UPDATE (customer) ON shop.orders TO reporting;The column-level grant does not grant access to other columns. A role may therefore be able to update customer without being able to update total.
Sequences and functions need separate checks
Identity columns and serial columns use sequences. A role inserting into such a table may need sequence privileges in addition to table privileges:
GRANT USAGE, SELECT ON SEQUENCE shop.orders_id_seq TO reporting;The exact sequence name depends on how the table was created. Inspect it before granting access.
Functions require EXECUTE. Calling a function does not automatically grant the caller privileges on tables that the function reads. Under the default security-invoker behavior, the function runs with the caller's permissions.
GRANT EXECUTE ON FUNCTION shop.calculate_total(numeric, numeric) TO reporting;PostgreSQL identifies overloaded functions by their argument types, so include the parameter list when granting on a specific function.
Exercise
A role has SELECT on shop.orders but receives a permission denied error when querying shop.orders. What privilege is probably missing?
Show answer
USAGE on the shop schema. Table SELECT and schema USAGE are separate privileges.