postgres new schema to inherit permissions for a user - sql

I have a created read only user with below commands
CREATE ROLE read_only WITH LOGIN PASSWORD 'password'
NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION VALID UNTIL
'infinity';
Then I gave select permision
GRANT USAGE ON SCHEMA public TO read_only;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO read_only;
GRANT SELECT ON ALL TABLES IN SCHEMA public to read_only;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO
read_only;
My requirement is whenever there is a new schema added it should inherit the permissions for read_only user. Currently I manually assign these permissions for new schema.

There is no way to do that automatically, you will have to continue doing that explicitly.

Related

Why does new role already have access to some databases in snowflake?

create role aaaaa_min_access_role;
grant role aaaaa_min_access_role to user me;
use role aaaaa_min_access_role;
show grants to role aaaaa_min_access_role;
show databases;
it results in 3 databases being outputted, the owner of those is accountadmin
wondering how?
when i do
REVOKE USAGE ON DATABASE DB_TEST FROM ROLE aaaaa_min_access_role;
REVOKE USAGE ON SCHEMA DB_TEST.SCHEMA FROM ROLE aaaaa_min_access_role;
REVOKE SELECT ON ALL TABLES IN SCHEMA DB_TEST.SCHEMA FROM ROLE aaaaa_min_access_role;
it results in 0 objects being impacted. I'm able to execute select * from DB_TEST.SCHEMA.Table1 successfully
Unless specified otherwise, new roles inherit from the PUBLIC role.
PUBLIC
Pseudo-role that is automatically granted to every user and every role in your account.
https://docs.snowflake.com/en/user-guide/security-access-control-overview.html#system-defined-roles
So the new role probably had at least some access to these databases through the PUBLIC role. You can check that by revoking the PUBLIC role from the new role you created.

postgres: how to create role with insert and update access

I have a user db_owner who is owner to my database called 'Sales'.
Now i have to create two groups(sales_ro and sales_riu) and then i will add users to this groups.
sales_ro group should inherit(from db_owner) read access on tables and execute on functions in Sales db
sales_riu group should inherit(from db_owner) insert and update access on tables and execute on functions in Sales db.
can we create such two groups in Postgres ?
You don't need to create groups to achieve this. You can just create Roles and assign them to the users you want. For example:
CREATE ROLE sales_ro;
CREATE ROLE sales_riu;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO sales_ro;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO sales_ro;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO sales_ro;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT EXECUTE ON FUNCTIONS TO sales_ro;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT,INSERT,UPDATE ON TABLES TO sales_riu;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT EXECUTE ON FUNCTIONS TO sales_riu;
After that just grant role to expected users:
GRANT sales_ro TO your_user_1;
GRANT sales_riu TO your_user_2;
Please refer link 1 and link 2 to know more about ALTER DEFAULT PRIVILEGES and CREATE ROLE respectively.
Quoting below points from above links:
CREATE ROLE adds a new role to a PostgreSQL database cluster. A role
is an entity that can own database objects and have database
privileges; a role can be considered a “user”, a “group”, or both
depending on how it is used.
A role having the LOGIN attribute can be thought of as a user. Roles
without this attribute are useful for managing database privileges

User management + grant

I have a database with multiple tables, functions, sequences, and types inside.
I want to create multiple "administrator"-like users:
they all might have all privileges to manage everything (existing and newly created items) in this database;
they all have the same privileges;
they might cross-manage entities (table created by administrator a should be available for a removal for administrator b)
I tried to create something like this, but failed on types:
"GRANT ALL PRIVILEGES ON DATABASE {dbname} TO {rw_user};"
"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO {rw_user};"
"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO {rw_user};"
"GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO {rw_user};"
"GRANT ALL PRIVILEGES ON ALL TYPES IN SCHEMA public TO {rw_user};" --not exists
"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON TABLES TO {rw_user};"
"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON SEQUENCES TO {rw_user};"
"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON FUNCTIONS TO {rw_user};"
"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON TYPES TO {rw_user};"
The only way I can think of to do that (if you don't want a shared database user) is the following:
CREATE ROLE admins;
CREATE ROLE a LOGIN NOINHERIT IN ROLE admins;
CREATE ROLE b LOGIN NOINHERIT IN ROLE admins;
CREATE SCHEMA shared;
GRANT CREATE; USAGE ON SCHEMA shared TO admins;
Then neither a nor b can create objects in shared unless they do the following:
SET ROLE admins;
CREATE TABLE shared.newtab (...);
RESET ROLE;
Such a table is then owned by admins, and both a and b can ALTER or DROP it.
Note that ALTER and DROP are restricted to the owner of the object, and you cannot GRANT these rights.

Preventing users from altering default privileges on PostgreSQL

I'm testing database permissions on PostgreSQL and i'm trying to prevent a common user (readuser) from executing an 'ALTER DEFAULT PRIVILEGES' statement. However i cannot find a way to revoke this specific permission, and couldn't find anything about it on documentation.
I started a local PostgreSQL 11.2 instance, removed connect permisssions, created a database testdb and revoked table creation on the public schema.
revoke connect on database postgres from public;
create database testdb with template template0 --lc_collate "pt_BR.utf8" lc_ctype "pt_BR.utf8";
revoke connect on database testdb from public;
\c :database
revoke all on schema public from public;
grant all on schema public to postgres;
create schema private;
After that, I created a user with read permissions only:
create user readuser
with nosuperuser
nocreatedb
nocreaterole
noreplication
login
encrypted password 'testpassword';
grant connect
on database testdb
to readuser;
Then create a schema testschema and granted read permissions on it's tables:
grant usage
on schema testschema
to readuser;
grant select
on all tables
in schema testschema
to readuser;
Even though i only set read permissions on all schemas and tables, the 'readuser' user can still perform 'alter default privileges' query without a permission error:
alter default privileges in schema testschema grant select on tables to readuser;
ALTER DEFAULT PRIVILEGES
I would like some help on preventing a user from altering it's default privileges, so that it cannot mess up permissions for tables created in the future.
Try this by revoking the EXECUTE from the role postgres that granted the default privilege of execute to readuser
ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA testschema REVOKE EXECUTE ON FUNCTIONS FROM readuser;

how can I create role groups in postgresql

I can create a role in postgresql.
CREATE ROLE myname WITH LOGIN PASSWORD 'pass';
and I can set privilages on a database schema for this user.
GRANT USAGE ON SCHEMA public TO myname;
and select privilages to a user.
GRANT SELECT ON ALL TABLES IN SCHEMA public TO myname;
But I have so many users in my database. I do not want to set these privilages to all of my users. Actually I want to create role groupnames:
viewer
editor
admin
And
viewer will be select privilages on all tables,
editor will be select, insert and update privilages on all tables.
my users will be in these groups.
How can I do this?
CREATE ROLE viewer;
CREATE ROLE editor;
CREATE ROLE admin;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO viewer;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO viewer;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT,INSERT,UPDATE ON TABLES TO editor;
GRANT some_other_privs_to_admin_group
after that just grant group to user:
GRANT editor TO your_user;
and so on
https://www.postgresql.org/docs/current/static/sql-alterdefaultprivileges.html
https://www.postgresql.org/docs/current/static/sql-createrole.html
CREATE ROLE adds a new role to a PostgreSQL database cluster. A role
is an entity that can own database objects and have database
privileges; a role can be considered a “user”, a “group”, or both
depending on how it is used.
and
A role having the LOGIN attribute can be thought of as a user. Roles
without this attribute are useful for managing database privileges
For this very reason it is advisable to use "groups", that is roles (usually with NOLOGIN) to which you add the users (by granting the role to them).
In your case:
CREATE ROLE viewer;
GRANT <whatever> TO viewer;
GRANT viewer TO myname;
Then myname will enjoy all the privileges granted to viewer, and you don't have to mess around with granting and revoking privileges to every user.