Grant access privileges on objects created in the future
suggest changeSuppose that we have three users:
- The administrator of the database:
admin - The application, with full access to its data:
read_write - Read-only access:
read_only
ALTER DEFAULT PRIVILEGES does not change objects that already exist. Without FOR ROLE, it applies only to objects later created by the role that runs the command.
With the queries below, you can set privileges on tables created in the future in a schema:
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT SELECT ON TABLES TO read_only;
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT SELECT,INSERT,DELETE,UPDATE ON TABLES TO read_write;
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT ALL ON TABLES TO admin;
Or, you can set access privileges on objects created in the future by specified user.
ALTER DEFAULT PRIVILEGES FOR ROLE admin GRANT SELECT ON TABLES TO read_only;
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents