Grant READ ONLY permissions to all tables in all databases - postgresql

I created new role named "support" in my PostgreSQL. Now I need grant "READ ONLY" permissions for this role an ALL exists databases/tables.
Also I need automatically granted same permissions on each DB that will created in future.
I unsuccessfully tried next queries for grant permissions in new databases (Can not select from new database tables ):
ALTER DEFAULT PRIVILEGES FOR ROLE support GRANT SELECT ON TABLES TO PUBLIC;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES to support;

ALTER DEFAULT PRIVILEGES allows you to set the privileges that will be
applied to objects created in the future. (It does not affect
privileges assigned to already-existing objects.)

Related

PostgreSQL export/import in Google cloud: issues with roles and users

I am running this command
gcloud sql import sql db1 gs://mybucket/sqldumpfile.gz --database=mydb1
to import a database snapshot into a new database. Before running it, I recreated the same users I had in the source database, using Cloud Console. However, I keep on getting this error:
ERROR: must be member of role "postgres"
STATEMENT: ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT SELECT ON TABLES TO user1;
I am not sure what to do and which user must be "member of role postgres".
Any advice is appreciated
To grant default privileges for user2, use the FOR ROLE clause:
ALTER DEFAULT PRIVILEGES FOR USER <user-1> IN SCHEMA <user-1> GRANT INSERT, UPDATE, DELETE ON TABLES TO <user-2>;
ALTER DEFAULT PRIVILEGES FOR USER <user-1> IN SCHEMA <user-1> GRANT SELECT ON TABLES TO <user-2>;
You need to grant the rights from the user-1 which is creating the table, So whenever the user-1 creates a table, it will grant the SELECT rights for the user-2.
For more information refer to this document.

Postgres - how can I restrict the privilege to create/drop a table?

I would like to create "read-only privileges" in a PostgreSQL database (including the restriction of creating or dropping tables).
My strategy is to create a group with these privileges and then add roles that have had all their privileges revoked. In that way, the only inherit privileges when part of the read-only group.
I used the following commands to create privileges but it seems roles can add, delete tables when they join the group:
role_test_db=# REVOKE ALL ON DATABASE role_test_db FROM select_access_group;
REVOKE
role_test_db=# GRANT CONNECT ON DATABASE role_test_db TO select_access_group;
GRANT
role_test_db=# GRANT SELECT ON ALL TABLES IN SCHEMA public TO select_access_group;
GRANT
I was reading the documentation and it seems like creating tables would be under CREATE privilege but I have not granted this. Can some explain why users part of this group can still make tables?
There are several mistakes:
Revoking privileges on the database does not restrict user's rights to create objects. For that, you have to revoke privileges on the schemas.
You can only REVOKE privileges that were GRANTed (by default or explicitly). I doubt that select_access_group has ever been granted any privileges on the database.
You likely forgot to revoke the dangerous default CREATE privilege on schema public. Connect as superuser and run
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
A user can only revoke privileges that were granted directly by that user
https://www.postgresql.org/docs/13/sql-revoke.html
See privileges
\du
select * from pg_roles;
Change (base) prilileges under admin role (postgres)

PostgreSQL drop role with default privileges

I am attempting to drop a role and have severed all ties to it that I can locate, but there is one lingering issue I cannot resolve. When I run this:
drop role hank
It tells me:
ERROR: role "hank" cannot be dropped because some objects depend on it
DETAIL: privileges for default privileges on new functions belonging to role brandon in schema alteryx
privileges for default privileges on new relations belonging to role brandon in schema alteryx
This DDL exists on the schema:
ALTER DEFAULT PRIVILEGES IN SCHEMA alteryx
GRANT INSERT, SELECT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER ON TABLES
TO hank;
ALTER DEFAULT PRIVILEGES IN SCHEMA alteryx
GRANT EXECUTE ON FUNCTIONS TO hank;
And when I execute the revoke on them, the command is successful, but the privileges remain intact.
I have scoured the DDL and can't locate how to resolve this without attempting a drop-cascade.
Any guidance is welcome.
You have to run the following two statements to get rid of the default privileges that block you:
ALTER DEFAULT PRIVILEGES FOR ROLE brandon IN SCHEMA alteryx
REVOKE ALL ON TABLES FROM hank;
ALTER DEFAULT PRIVILEGES FOR ROLE brandon IN SCHEMA alteryx
REVOKE ALL ON FUNCTIONS FROM hank;

Revoke temp on database from role postgres

I am trying to create a read-only user in PostgreSQL and I have done so, with the only caveat being that my new read-only user is able to create temporary tables. How?? Why??
I have specifically run:
CREATE ROLE read_access LOGIN;
REVOKE CREATE ON SCHEMA public FROM public;
GRANT USAGE ON SCHEMA {schema_name} TO read_access;
GRANT SELECT ON ALL TABLES IN SCHEMA {schema_name} TO read_access;
ALTER DEFAULT PRIVILEGES IN SCHEMA {schema_name}
GRANT SELECT ON TABLES TO read_access;
You are missing one permission:
REVOKE TEMPORARY ON DATABASE {dbname} FROM PUBLIC;
By default, the special role PUBLIC, to which everybody automatically belongs, is allowed to create temporary tables.

PostgreSQL and privileges

How does privileges for new relations in PostgreSQL work?
Steps:
Create DB (from user postgres) and connect to it
CREATE DATABASE test;
\c test
Create user site with some privileges
CREATE USER site NOCREATEDB NOINHERIT;
GRANT SELECT, UPDATE, INSERT, DELETE, TRUNCATE, REFERENCES ON ALL TABLES IN SCHEMA public TO site;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO site;
Change default privileges for user site
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, UPDATE, INSERT, DELETE, TRUNCATE, REFERENCES ON TABLES TO site;
Create user migration with all privileges
CREATE USER migration NOCREATEDB NOINHERIT;
GRANT ALL PRIVILEGES ON DATABASE test TO migration;
Connect to DB from user migration and create table
CREATE TABLE test (id serial);
Connect to DB from user site and select data from created table
SELECT * FROM test;
ERROR: permission denied for relation test
But if I create table from user postgres, all work fine!
Why default privileges didn't work in this case? How can I grant permissions for new tables for user site?
ALTER DEFAULT PRIVILEGES only affects objects created by the user specified in the FOR ROLE clause. If you omit this clause, it only applies to the user running the command (in your case, postgres).
You want ALTER DEFAULT PRIVILEGES FOR USER migration ... instead.