Sub topic 4
The privilege types and which objects they apply to
This chapter assumes you know what a role is, the difference between an owner and a role that has been granted access, and the basic form GRANT privilege ON object TO role. Here you learn which privilege names exist, which object types accept each one, and what each privilege actually permits. Examples use the shop schema and roles from earlier chapters.
Privileges are tied to object types
A privilege is a named permission on one kind of object. SELECT makes sense on a table but not on a database, and CONNECT makes sense on a database but not on a table. If you grant a privilege on an object type that does not accept it, PostgreSQL raises an error such as invalid privilege type SELECT for schema.
| Privilege | ACL letter | Applies to |
|---|---|---|
| SELECT | r | tables, views, columns, sequences, large objects |
| INSERT | a | tables, columns |
| UPDATE | w | tables, columns, sequences, large objects |
| DELETE | d | tables |
| TRUNCATE | D | tables |
| REFERENCES | x | tables, columns |
| TRIGGER | t | tables |
| MAINTAIN | m | tables (PostgreSQL 17 and later) |
| USAGE | U | schemas, sequences, types and domains, languages, foreign data wrappers, foreign servers |
| CREATE | C | databases, schemas, tablespaces |
| CONNECT | c | databases |
| TEMPORARY | T | databases |
| EXECUTE | X | functions, procedures, routines |
| SET, ALTER SYSTEM | s, A | configuration parameters (PostgreSQL 15 and later) |
The letter column is how privileges appear in the aclitem format. You see it in \dp output and in the relacl catalog column. For example, app_readonly=r/app_owner means appowner granted SELECT to appreadonly.
Two operations are missing from the table: altering and dropping an object. No privilege covers them. They belong to the owner (and to members of the owner role), and you cannot grant them separately.
Table privileges in detail
- SELECT reads rows. It is also needed by any
UPDATEorDELETEwhoseWHEREclause (orSETexpression) reads column values, and byINSERT ... RETURNING. - INSERT adds rows.
INSERT ... ON CONFLICT DO UPDATEneedsINSERTandUPDATE, plusSELECTon any column it reads. - UPDATE changes rows.
- DELETE removes rows.
- TRUNCATE empties the table. It is separate from
DELETE, so a role withDELETEcannot truncate. - REFERENCES allows creating a foreign key that points at the table or column. It is rarely granted.
- TRIGGER allows creating triggers on the table.
- MAINTAIN (PostgreSQL 17 and later) allows
VACUUM,ANALYZE,CLUSTER,REFRESH MATERIALIZED VIEW,REINDEX, andLOCK TABLE.
ALL PRIVILEGES is shorthand for every privilege valid for that object type. It changes meaning between server versions as new privileges appear, so avoid it in scripts you want to keep reviewable.
The SELECT requirement for UPDATE with a WHERE clause surprises many people:
CREATE ROLE batch NOLOGIN;
GRANT USAGE ON SCHEMA shop TO batch;
GRANT UPDATE ON shop.orders TO batch;
SET ROLE batch;
UPDATE shop.orders SET customer = 'Acme' WHERE id = 1;
-- ERROR: permission denied for table orders
UPDATE shop.orders SET customer = 'Acme';
-- works: no column values are read
RESET ROLE;The first statement fails because WHERE id = 1 reads id, which needs SELECT. The second statement reads nothing, so UPDATE alone is enough. In practice, grant SELECT together with UPDATE and DELETE.
Column privileges
SELECT, INSERT, UPDATE, and REFERENCES can be granted on individual columns:
GRANT SELECT (id, customer) ON shop.orders TO alice;Alice can now run SELECT id, customer FROM shop.orders. SELECT * fails, because * expands to every column including ones she cannot read. A table-level grant covers all columns, including any added later. A column-level grant covers only the named columns. If you later want to remove a table-level grant while keeping specific columns, revoke the table-level privilege first and then grant the columns.
Schema, database, and other object privileges
Schemas accept two privileges. USAGE lets a role look up objects inside the schema by name. CREATE lets it create new objects there. Without USAGE on the schema, table privileges are unreachable:
-- alice has SELECT on shop.orders but no USAGE on schema shop
SELECT * FROM shop.orders;
-- ERROR: permission denied for schema shopDatabases accept CONNECT, TEMPORARY, and CREATE. CONNECT controls who can open a session in the database. TEMPORARY allows creating temporary tables. CREATE allows creating schemas in the database, not tables. Tables are created inside schemas, so they need CREATE on a schema.
Sequences accept three privileges:
USAGEallowscurrvalandnextval.SELECTallowscurrvaland reading the sequence's state.UPDATEallowsnextvalandsetval.
CREATE SEQUENCE shop.invoice_no;
GRANT USAGE ON SEQUENCE shop.invoice_no TO app_writer;If a column uses DEFAULT nextval('shop.invoice_no'), including a serial column, the inserting role needs USAGE on the sequence. Identity columns, such as id in shop.orders, do not check sequence privileges. That is why the default-privileges example in the spine grants sequence USAGE to the writer: it covers tables that use serial or an explicit nextval default.
Functions and procedures accept only EXECUTE. The syntax names the signature when overloads exist:
GRANT EXECUTE ON FUNCTION shop.order_total(bigint) TO app_readonly;Use ON ROUTINE to match a function or procedure by name without specifying which kind it is.
Other objects follow the table at the top of the chapter: USAGE on types, domains, languages, and foreign servers. CREATE on tablespaces. SET and ALTER SYSTEM on configuration parameters. SELECT and UPDATE on large objects.
Defaults that PUBLIC already holds
A new object does not start with no access for everyone. PUBLIC, the pseudo-role that stands for every role, gets some privileges automatically:
| Object | Default privileges for PUBLIC |
|---|---|
| Database | CONNECT, TEMPORARY |
| Function, procedure | EXECUTE |
| Language | USAGE |
| Type, domain | USAGE |
| Table, sequence, schema | none (except `public` schema usage) |
Tables, sequences, and new schemas begin with no access for anyone but the owner. Functions and databases are open by default. A later chapter covers how to revoke these defaults.
Reading privileges back
\dp shop.orders in psql shows the access privileges column. A table owned by app_owner with a read-only grant looks like this:
app_owner=arwdDxt/app_owner
app_readonly=r/app_ownerThe first entry shows the owner holding every table privilege (plus m on PostgreSQL 17 and later). The second shows SELECT granted by appowner. To test a specific privilege, use the `has*_privilege` functions, which respect inheritance and PUBLIC:
SELECT has_table_privilege('alice', 'shop.orders', 'SELECT') AS table_select,
has_column_privilege('alice', 'shop.orders', 'total', 'SELECT') AS column_select,
has_schema_privilege('alice', 'shop', 'USAGE') AS schema_usage,
has_sequence_privilege('alice', 'shop.invoice_no', 'USAGE') AS seq_usage,
has_function_privilege('alice', 'shop.order_total(bigint)', 'EXECUTE') AS can_run;Each function returns a boolean. Append WITH GRANT OPTION to a privilege name, as in 'SELECT WITH GRANT OPTION', to test whether the role can pass the privilege on.
Common errors
permission denied for schema: the role lacksUSAGEon the schema, whatever its table privileges.permission denied for tableon anUPDATEorDELETE: the statement reads columns, so it also needsSELECT.permission denied for sequence: the table uses aserialornextvaldefault and the inserting role lacksUSAGEon the sequence.must be owner of tableonALTER TABLEorDROP TABLE: no privilege grants these. Use the owner role, or add the role to the owner's group.permission denied to create table: the role lacksCREATEon the schema.CREATEon the database does not help.
Exercise
A role has SELECT on shop.orders and has UPDATE (customer) on the same table, but no USAGE on schema shop. It runs UPDATE shop.orders SET customer = 'x' WHERE id = 1. Name every privilege problem that stops this statement.
Show answer
The role lacks USAGE on schema shop, so the statement fails with "permission denied for schema shop" before any table check. Even with USAGE added, the WHERE id = 1 clause reads the id column, which the role can only read if its SELECT grant covers that column. Here SELECT is table-level, so that part passes and UPDATE (customer) covers the assigned column. The only missing privilege is schema USAGE.
Exercise
Which privilege lets a role call nextval on a sequence but not call setval?
Show answer
USAGE on the sequence. UPDATE would also allow nextval, but it additionally allows setval, so USAGE is the narrower grant.