Sub topic 11
ALTER DEFAULT PRIVILEGES for future objects
This chapter assumes you know GRANT and REVOKE, object ownership, and group roles. It continues the shop schema example, where app_owner owns the objects, app_readonly and app_writer are group roles, and alice is a member of app_readonly.
The problem: grants only see existing objects
GRANT SELECT ON ALL TABLES IN SCHEMA shop TO app_readonly reads the list of tables when the command runs. A table created tomorrow is not on that list. The new table has only its built-in default access, which is the owner and nobody else. A migration adds a table, the application starts failing with permission denied for table, and someone has to grant access by hand.
ALTER DEFAULT PRIVILEGES fixes this. It stores a rule that says: when role R creates an object of this kind, apply these grants to it.
Syntax and what it controls
ALTER DEFAULT PRIVILEGES
[ FOR ROLE creator_role [, ...] ]
[ IN SCHEMA schema_name [, ...] ]
GRANT privileges ON object_kind TO grantee [, ...];REVOKE works the same way. Three parts decide how a rule behaves:
FOR ROLEnames the role whose future objects are affected. If you leave it out, the rule applies to objects created by the role running the command. It does not apply to objects created by other roles. You must be that role, a member of it, or a superuser.IN SCHEMAlimits the rule to one schema. If you leave it out, the rule applies to objects the role creates in any schema of the current database.- The object kind is one of the keywords in the table below.
| Keyword | Objects covered | Typical privileges |
|---|---|---|
| `TABLES` | Tables, views, materialized views, foreign tables, partitioned tables | `SELECT`, `INSERT`, `UPDATE`, `DELETE` |
| `SEQUENCES` | Sequences | `USAGE`, `SELECT`, `UPDATE` |
| `FUNCTIONS` | Functions, procedures, aggregates | `EXECUTE` |
| `TYPES` | Types and domains | `USAGE` |
| `SCHEMAS` | Schemas | `USAGE`, `CREATE` |
The rule changes nothing that exists today. It only affects objects created after the command.
Applying the rules to the shop schema
These statements make every table and sequence that app_owner creates in shop carry the right grants:
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT SELECT ON TABLES TO app_readonly;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT SELECT, INSERT, UPDATE ON TABLES TO app_writer;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT USAGE ON SEQUENCES TO app_writer;Test the rules by creating a table as the owner and asking PostgreSQL what the group roles can do:
SET ROLE app_owner;
CREATE TABLE shop.invoices (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
order_id bigint NOT NULL,
amount numeric(10,2) NOT NULL
);
RESET ROLE;
SELECT has_table_privilege('app_readonly', 'shop.invoices', 'SELECT') AS ro_select,
has_table_privilege('app_writer', 'shop.invoices', 'INSERT') AS rw_insert,
has_table_privilege('app_readonly', 'shop.invoices', 'INSERT') AS ro_insert;The result is true, true, false. No GRANT ran after CREATE TABLE.
Rules cover existing objects only if you also grant to them once. A complete setup therefore has two parts: GRANT ... ON ALL TABLES for what exists now, and ALTER DEFAULT PRIVILEGES for what comes later.
Sequences and the identity column
Tables with GENERATED ... AS IDENTITY do not need the sequence rule. An INSERT into such a table needs only INSERT on the table, because the sequence belongs to the column. A table that uses serial or bigserial, or code that calls nextval() directly, needs USAGE on the sequence. The sequence rule above covers those cases. Keep it even if you use identity columns today, since the schema may later hold tables written in the older style.
Functions: PUBLIC gets EXECUTE by default
New functions and procedures grant EXECUTE to PUBLIC, so every role can run them. This is the one built-in default that is easy to miss. Remove it with a default privilege rule, then grant back what you want:
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner
REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT EXECUTE ON FUNCTIONS TO app_writer;The first statement has no IN SCHEMA, so it applies to every schema where app_owner creates functions. The second adds a grant for shop only.
Global and per-schema rules combine
A rule with IN SCHEMA is added to the global rule for the same role and object kind. It never replaces it. This has one consequence you need to remember: a schema-level REVOKE cannot remove a privilege that a global rule grants. It can only undo a schema-level GRANT. To remove something a global rule grants, revoke it at the global level.
Inspecting the rules
In psql, \ddp lists default privilege rules. The catalog pg_default_acl holds the same data, which is easier to filter in a script:
SELECT defaclrole::regrole AS creator,
defaclnamespace::regnamespace AS schema,
defaclobjtype AS kind,
defaclacl AS acl
FROM pg_default_acl
ORDER BY 1, 2, 3;The kind column uses one letter per object kind: r for tables, S for sequences, f for functions, T for types, and n for schemas. A schema value of - means the rule is global for that role. Each rule is stored for one database. If you use several databases, repeat the rules in each one.
Edge cases and failure modes
- Existing objects stay as they are. Adding a rule after a hundred tables exist changes none of them. Pair the rule with a one-time
GRANT ... ON ALL TABLES IN SCHEMAandGRANT ... ON ALL SEQUENCES IN SCHEMA. - Removing a rule does not revoke grants.
ALTER DEFAULT PRIVILEGES ... REVOKEstops future objects from receiving the grant. Tables that already received it keep it until you revoke it withREVOKE. - A superuser creating objects does not use your owner's rules. Objects created by
postgresare governed by rules forpostgres. This is a common reason for "the rule exists but the table is not readable". - Dropping a role needs cleanup. Default privilege entries that mention a role block
DROP ROLE.DROP OWNED BYremoves them, as covered in the chapter on dropping roles. - Views follow the table rule. A new view gets the
TABLESgrants. Remember that a view runs with its owner's privileges on the underlying tables, so a readable view can expose data the reader could not select directly.
Summary of the working model
A default privilege rule is stored by (creator role, optional schema, object kind). When that role creates an object of that kind, PostgreSQL adds the global and schema rules to the built-in owner access. Anything created by another role, or before the rule, is untouched.
Exercise
A migration tool connects as the role deploy and creates shop.refunds. Your setup script ran ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop GRANT SELECT ON TABLES TO app_readonly. Can app_readonly read shop.refunds? Name two ways to fix it.
Show answer
No. The rule applies only to objects created by app_owner, and deploy created the table. One fix is to run ALTER DEFAULT PRIVILEGES FOR ROLE deploy IN SCHEMA shop GRANT SELECT ON TABLES TO app_readonly and then grant on the existing table with GRANT SELECT ON shop.refunds TO app_readonly. The other is to have the migration create tables as app_owner, for example by running SET ROLE app_owner first or by connecting as a role that is a member of app_owner.
Exercise
A global rule ALTER DEFAULT PRIVILEGES FOR ROLE app_owner GRANT SELECT ON TABLES TO app_readonly exists. You then run ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop REVOKE SELECT ON TABLES FROM app_readonly so that new tables in shop are hidden from app_readonly. Does it work? How would you check?
Show answer
No. Schema-level rules are added to global rules, so the global grant still applies to new tables in shop. A schema-level revoke only cancels a schema-level grant. To check, create a table as app_owner in shop and run has_table_privilege('app_readonly', 'shop.new_table', 'SELECT'), which returns true. To fix it, revoke the global rule with ALTER DEFAULT PRIVILEGES FOR ROLE app_owner REVOKE SELECT ON TABLES FROM app_readonly, then add schema-level grants for the schemas that should keep it. You can also inspect pg_default_acl to see both entries.