Sub topic 11
ALTER DEFAULT PRIVILEGES for Future Objects
Assume you know basic SQL and can connect to PostgreSQL. This chapter also assumes that app_owner, app_readonly, app_writer, and the shop schema already exist. The goal is to make new objects receive the intended privileges automatically.
A normal GRANT changes privileges on objects that already exist. ALTER DEFAULT PRIVILEGES changes the privileges assigned when a role creates future objects.
| Command | Applies to |
|---|---|
| `GRANT SELECT ON ALL TABLES` | Existing tables only |
| `ALTER DEFAULT PRIVILEGES ... GRANT SELECT ON TABLES` | Future tables created by a specific role |
Why the creating role matters
Default privileges belong to the role that creates the object. They do not automatically follow role membership.
For example, if alice becomes a member of app_owner and creates a table while using her own identity, defaults configured only for app_owner do not apply. If the object must use app_owner's defaults, create it while acting as app_owner with SET ROLE, or configure defaults for the actual creating role.
The IN SCHEMA clause limits the defaults to one schema. Without it, the defaults apply to objects created by that role in every schema where the command applies.
Configure future table and sequence privileges
Run these commands as app_owner, a superuser, or a role allowed to change the target role's defaults:
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;The first command gives app_readonly SELECT on each future table created by app_owner in shop. The second gives app_writer read and write privileges on those tables. The third allows writers to use sequences, including sequences behind identity or serial columns.
These commands do not grant schema access. A member still needs USAGE on shop:
GRANT USAGE ON SCHEMA shop TO app_readonly, app_writer;They also do not change existing tables. Apply a separate grant when current objects need access:
GRANT SELECT ON ALL TABLES IN SCHEMA shop TO app_readonly;
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA shop TO app_writer;Understand the scope
Default privileges affect only objects created after the default is configured. They are also specific to the object type and creator.
Use REVOKE to remove a default grant. This changes future objects only:
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
REVOKE UPDATE ON TABLES FROM app_writer;It does not remove UPDATE from tables that already exist. Use an ordinary REVOKE for those tables.
Check the configuration
In psql, use \ddp to list default privileges. To inspect privileges on existing tables, use \dp. Catalog views such as information_schema.role_table_grants show grants already applied to objects, not merely planned defaults.
Exercise
A new table created by appowner in shop does not give appwriter INSERT access. What should you check first?
Show answer
Check that the default privilege was configured FOR ROLE app_owner, that it includes INSERT ON TABLES, that the table is in shop, and that the table was created after the default was configured.