I'm trying to create two users in a Postgres database—one with read-write access to all tables in two schemas, and one with read-write-create (i.e., able to make DDL changes) to the same schemas.
I currently have these statements.
CREATE SCHEMA
schema_a;
CREATE SCHEMA
schema_b;
CREATE ROLE read;
CREATE ROLE read_write;
CREATE ROLE read_write_create;
GRANT USAGE ON SCHEMA schema_a, schema_b TO read;
GRANT SELECT ON ALL TABLES IN SCHEMA schema_a, schema_b TO read;
GRANT INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA schema_a, schema_b TO read_write;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA schema_a, schema_b TO read_write;
GRANT ALL ON SCHEMA schema_a, schema_b TO read_write_create;
GRANT read to read_write;
GRANT read_write to read_write_create;
ALTER DEFAULT PRIVILEGES IN SCHEMA schema_a, schema_b GRANT SELECT ON TABLES TO read;
ALTER DEFAULT PRIVILEGES IN SCHEMA schema_a, schema_b GRANT SELECT ON SEQUENCES TO read;
ALTER DEFAULT PRIVILEGES IN SCHEMA schema_a, schema_b GRANT INSERT, UPDATE, DELETE ON TABLES TO read_write;
ALTER DEFAULT PRIVILEGES IN SCHEMA schema_a, schema_b GRANT SELECT, UPDATE ON SEQUENCES TO read_write;
CREATE USER
read_write_user
WITH PASSWORD
'a_password';
GRANT read_write TO read_write_user;
CREATE USER
read_write_create_user
WITH PASSWORD
'another_password';
GRANT read_write_create TO read_write_create_user;
After running these, read_write_create_user can create tables, read from, and write to all tables. But read_write_user can't read or write to any tables created by read_write_create_user.
What am I doing wrong?
ALTER DEFAULT PRIVILEGES FOR ROLE read_write_create IN SCHEMA schema_a, schema_b..., but I still have the same issue. Is there something else I need to change? – Kris Harper Mar 20 '19 at 19:06ALTER DEFAULT PRIVILEGES FOR USER read_write_create_user IN SCHEMA schema_a, schema_b...? – Kris Harper Mar 20 '19 at 20:29ALTER DEFAULT PRIVILEGES FOR USER read_write_create_user IN SCHEMA schema_a, schema_b...TO read_write_user, read_write_create_userbut that didn't work either. – Kris Harper Mar 20 '19 at 22:27ALTER DEFAULT PRIVILEGES..? – jjanes Mar 20 '19 at 23:45set role read_write_create;before creating things. – jjanes Mar 20 '19 at 23:52ALTER DEFAULTstatements had to be executed byread_write_create_user. Do you have any links to some best practices on this? I am new to Postgres and to be honest I find it very confusing that permissions for tables depend on which user created the table (and which user created the permission, even). – Kris Harper Mar 21 '19 at 02:08