I have a readonly
user that I defined as the following:
CREATE ROLE readonly;
GRANT CONNECT ON DATABASE dbname TO readonly;
GRANT USAGE ON SCHEMA public TO readonly;
GRANT USAGE ON SCHEMA schema_name TO readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA schema_name TO readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA schema_name GRANT SELECT ON TABLES TO readonly;
I am currently running these commands as the postgres
user who is the rds_superuser
(I'm using AWS RDS). This works fine, and allows readonly
role to read existing tables that postgres
user creates, and also any new tables that postgres
creates. However, when a new role creates a table, readonly
cannot SELECT
the table that the new role creates. For example, I also have a readwrite
role, and tables created in that schema by readwrite
cannot be SELECT
by readonly
.
How do you create a read-only user in PostgreSQL? suggests that by default ALTER DEFAULT PRIVILEGES ...
only works by default on objects created by the user that issued the command.
How can I more generally just allow readonly
user to have SELECT
privileges on ANY table created in the schema regardless of who creates it.