Grant select on future views snowflake - sql

How can I grant select on all future views in a schema or database.
This works fine but I want the role to have access to all future views too:
grant select on view <schema>.<view> to role <role>;
Any ideas?

Grant on future objects like this (using ACCOUNTADMIN role):
Make sure USAGE is granted on database and schema:
GRANT USAGE ON DATABASE <database> TO ROLE <role>;
GRANT USAGE ON SCHEMA <database>.<schema> TO ROLE <role>;
Grant select on future objects:
grant select on future tables [views] in schema <database>.<schema> to role <role>;
grant select on future views in schema <database>.<schema> to role <role>;

Related

future grants on a snowflake database

Any insight on why the below grant statements are not providing future grants on tables in database db_prod for role analyst_legacy_test? Below grants do give select to views and tables, but not to future views or tables...(when a new table is created by another role, analyst_legacy_test role cannot see it or view it)
use role securityadmin;
grant usage on database db_prod to role analyst_legacy_test;
grant usage on all schemas in database db_prod to role analyst_legacy_test;
grant select on all tables in database db_prod to role analyst_legacy_test;
grant select on all views in database db_prod to role analyst_legacy_test;
grant usage on future schemas in database db_prod to role ANALYST_LEGACY_TEST;
grant select on future tables in database db_prod to role analyst_legacy_test;
grant select on future views in database db_prod to role ANALYST_LEGACY_TEST;
I've read multiple pieces of documentation and pulled these statements from there.
https://community.snowflake.com/s/article/How-to-grant-select-on-all-future-tables-in-a-schema-and-database-level
I've also read about the potential issue with future grant precedence (that if you designate schema level future grants the datbase level grants will be ignored). I don't believe my statements apply to this scenario
https://community.snowflake.com/s/article/Precedence-rule-for-future-grants
UPDATE/SOLUTION: The issue was that another role had schema level future grants. From the documentation it was not clear to me the future grants precedence issue spanned across roles. To resolve issue, I had to check every role in database for schema level grants (and remove them) or grant schema level grants on this role.
One plausible scenario is existence of another future grants that are assigned on schema level to different role. In such situation future grants assigned on the database level are ignored.
Considerations
When future grants are defined on the same object type for a database and a schema in the same database, the schema-level grants take precedence over the database level grants, and the database level grants are ignored. This behavior applies to privileges on future objects granted to one role or different roles.
Reproducible example:
USE ROLE SYSADMIN;
CREATE OR REPLACE DATABASE DB_PROD;
USE ROLE SECURITYADMIN;
CREATE OR REPLACE ROLE analyst_legacy_test;
grant usage on database db_prod to role analyst_legacy_test;
grant usage on all schemas in database db_prod to role analyst_legacy_test;
grant select on all tables in database db_prod to role analyst_legacy_test;
grant select on all views in database db_prod to role analyst_legacy_test;
grant usage on future schemas in database db_prod to role ANALYST_LEGACY_TEST;
grant select on future tables in database db_prod to role analyst_legacy_test;
grant select on future views in database db_prod to role ANALYST_LEGACY_TEST;
grant role ANALYST_LEGACY_TEST TO USER <user_name_here>;
USE ROLE SYSADMIN;
CREATE OR REPLACE SCHEMA TEST;
CREATE OR REPLACE VIEW TEST_VW AS SELECT 1 AS c;
-- no schema level future grants, the view is accessible
USE ROLE analyst_legacy_test;
SELECT * FROM TEST.TEST_VW;
-- 1
-- adding future grants to different role on schema level
USE ROLE SECURITYADMIN;
grant select on future views in schema DB_PROD.TEST to role sysadmin;
-- TEST_VW still works
USE ROLE analyst_legacy_test;
SELECT * FROM TEST.TEST_VW;
-- 1
-- new view
USE ROLE SYSADMIN;
CREATE OR REPLACE VIEW TEST.TEST_VW_NEW AS SELECT 2 AS c;
USE ROLE analyst_legacy_test;
SELECT * FROM TEST.TEST_VW_NEW;
-- Object 'DB_PROD.TEST.TEST_VW_NEW' does not exist or not authorized.

Permission denied for materialized view base schema

I get this error my when i'm selecting from an mview i created.
select * from mview_age_stats
This mview definition is using an external schema in its definition with the external schema "ext". I tried everything i could find online and gave permission at schema, table and every other level
ALTER DEFAULT PRIVILEGES IN SCHEMA ext GRANT SELECT ON TABLES TO my_user;
GRANT USAGE ON SCHEMA ext to my_user;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA ext TO my_user;
GRANT SELECT ON ALL TABLES IN SCHEMA ext TO my_user;
my_user is also in couple different groups, i gave the same permissions to each group and also more like this;
GRANT SELECT ON TABLE mview_age_stats TO GROUP read_only_group;
GRANT SELECT ON TABLE mview_age_stats TO GROUP read_write_only;
None of these worked, however what I noticed is that is i have a statement in my mview definition for transferring the ownership to a superuser - which my company uses in order keep ownership of all tables. If i remove the ownership it magically works but i don't understand how moving the ownership would make a difference since i'm granting permission to my_user anyway
alter table mview_age_stats
owner to main_user;

GRANT Privilege In SQL

Fast :
GRANT SELECT ON SYSTEM.* TO appadmin;
I want to grant AppAdmin the rights of SELECT on all tables of the database
I'm using Oracle SQL, why does my statement not work ?
Using the ANY keyword in reference to a system privilege means that the user can perform the privilege on any objects owned by any user except for SYS. By default, if you are granted a privilege, you cannot assign your privilege to others. You cannot grant or revoke that privilege to or from anyone else.
Sometimes you want to grant privileges to users and have them be able to grant those privileges to other users. When this is the case, we include the with admin keyword in the grant command. When this keyword is used, it will allow the user granted the privilege to grant that privilege to other users.
Here is an example of the usage of the with admin option keyword.
GRANT SELECT ANY TABLE TO User;
GRANT SELECT ANY TABLE TO YOUR_USER;

PostgreSQL: Give all permissions to a user on a PostgreSQL database

I would like to give a user all the permissions on a database without making it an admin.
The reason why I want to do that is that at the moment DEV and PROD are different DBs on the same cluster so I don't want a user to be able to change production objects but it must be able to change objects on DEV.
I tried:
grant ALL on database MY_DB to group MY_GROUP;
but it doesn't seem to give any permission.
Then I tried:
grant all privileges on schema MY_SCHEMA to group MY_GROUP;
and it seems to give me permission to create objects but not to query\delete objects on that schema that belong to other users
I could go on by giving USAGE permission to the user on MY_SCHEMA but then it would complain about not having permissions on the table ...
So I guess my question is: is there any easy way of giving all the permissions to a user on a DB?
I'm working on PostgreSQL 8.1.23.
All commands must be executed while connected to the right database cluster. Make sure of it.
Roles are objects of the database cluster. All databases of the same cluster share the set of defined roles. Privileges are granted / revoked per database / schema / table etc.
A role needs access to the database, obviously. That's granted to PUBLIC by default. Else:
GRANT CONNECT ON DATABASE my_db TO my_user;
Basic privileges for Postgres 14 or later
Postgres 14 adds the predefined, non-login roles pg_read_all_data / pg_write_all_data.
They have SELECT / INSERT, UPDATE, DELETE privileges for all tables, views, and sequences. Plus USAGE on schemas. We can GRANT membership in these roles:
GRANT pg_read_all_data TO my_user;
GRANT pg_write_all_data TO my_user;
This covers all basic DML commands (but not DDL, and not some special commands like TRUNCATE or the EXECUTE privilege for functions!). The manual:
pg_read_all_data
Read all data (tables, views, sequences), as if having SELECT rights
on those objects, and USAGE rights on all schemas, even without
having it explicitly. This role does not have the role attribute
BYPASSRLS set. If RLS is being used, an administrator may wish to
set BYPASSRLS on roles which this role is GRANTed to.
pg_write_all_data
Write all data (tables, views, sequences), as if having INSERT,
UPDATE, and DELETE rights on those objects, and USAGE rights on
all schemas, even without having it explicitly. This role does not
have the role attribute BYPASSRLS set. If RLS is being used, an
administrator may wish to set BYPASSRLS on roles which this role is
GRANTed to.
All privileges without using predefined roles (any Postgres version)
Commands must be executed while connected to the right database. Make sure of it.
The role needs (at least) the USAGE privilege on the schema. Again, if that's granted to PUBLIC, you are covered. Else:
GRANT USAGE ON SCHEMA public TO my_user;
Or grant USAGE on all custom schemas:
DO
$$
BEGIN
-- RAISE NOTICE '%', ( -- use instead of EXECUTE to see generated commands
EXECUTE (
SELECT string_agg(format('GRANT USAGE ON SCHEMA %I TO my_user', nspname), '; ')
FROM pg_namespace
WHERE nspname <> 'information_schema' -- exclude information schema and ...
AND nspname NOT LIKE 'pg\_%' -- ... system schemas
);
END
$$;
Then, all permissions for all tables (requires Postgres 9.0 or later).
And don't forget sequences (if any):
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO my_user;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO my_user;
Alternatively, you could use the "Grant Wizard" of pgAdmin 4 to work with a GUI.
This covers privileges for existing objects. To also cover future objects, set DEFAULT PRIVILEGES. See:
Grant privileges for a particular database in PostgreSQL
How to manage DEFAULT PRIVILEGES for USERs on a DATABASE vs SCHEMA?
There are some other objects, the manual for GRANT has the complete list. As of Postgres 14:
privileges on a database object (table, column, view, foreign table, sequence, database, foreign-data wrapper, foreign server, function, procedure, procedural language, schema, or tablespace)
But the rest is rarely needed. More details:
Grant privileges for a particular database in PostgreSQL
How to grant all privileges on views to arbitrary user
Consider upgrading to a current version.
GRANT ALL PRIVILEGES ON DATABASE "my_db" to my_user;
In PostgreSQL 9.0+ you would do the following:
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA MY_SCHEMA TO MY_GROUP;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA MY_SCHEMA TO MY_GROUP;
If you want to enable this for newly created relations too, then set the default permissions:
ALTER DEFAULT PRIVILEGES IN SCHEMA MY_SCHEMA
GRANT ALL PRIVILEGES ON TABLES TO MY_GROUP;
ALTER DEFAULT PRIVILEGES IN SCHEMA MY_SCHEMA
GRANT ALL PRIVILEGES ON SEQUENCES TO MY_GROUP;
However, seeing that you use 8.1 you have to code it yourself:
CREATE FUNCTION grant_all_in_schema (schname name, grant_to name) RETURNS integer AS $$
DECLARE
rel RECORD;
BEGIN
FOR rel IN
SELECT c.relname
FROM pg_class c
JOIN pg_namespace s ON c.namespace = s.oid
WHERE s.nspname = schname
LOOP
EXECUTE 'GRANT ALL PRIVILEGES ON ' || quote_ident(schname) || '.' || rel.relname || ' TO ' || quote_ident(grant_to);
END LOOP;
RETURN 1;
END; $$ LANGUAGE plpgsql STRICT;
REVOKE ALL ON FUNCTION grant_all_in_schema(name, name) FROM PUBLIC;
This will set the privileges on all relations: tables, views, indexes, sequences, etc. If you want to restrict that, filter on pg_class.relkind. See the pg_class docs for details.
You should run this function as superuser and as regular as your application requires. An option would be to package this in a cron job that executes every day or every hour.
I did the following to add a role 'eSumit' on PostgreSQL 9.4.15 database and provide all permission to this role :
CREATE ROLE eSumit;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO eSumit;
GRANT ALL PRIVILEGES ON DATABASE "postgres" to eSumit;
ALTER USER eSumit WITH SUPERUSER;
Also checked the pg_table enteries via :
select * from pg_roles;
Database queries snapshot :
In PostgreSQL 12 and later, it is possible to grant all privileges of a table in a database to a role/user/account.
The syntax is:
GRANT ALL ON table_name TO role_name;
If you want to grant it to all tables in the database then the syntax will be:
GRANT ALL ON ALL TABLES TO role_name;
If you want to grant it to all tables of a schema in the database then the syntax will be:
GRANT ALL ON ALL TABLES IN SCHEMA schema_name TO role_name;
Note: Remember you will need to select the database before you can grant its privileges to a user.
Resources: PostgreSQL GRANT
That's all
I hope this helps
GRANT USAGE ON SCHEMA schema_name TO user;
GRANT ALL ON SCHEMA schema_name TO user_name;
Give all permissions to a user on a PostgreSQL database:
Command:
grant all privileges on database [database_name] to [database_user_name];
Example:
grant all privileges on database studentdb to shaifullah;
OR
GRANT ALL PRIVILEGES ON DATABASE studentdb TO shaifullah;

Roles in oracle

I created a view in schema A using snapshots in schema B. I was trying to assign roles to the view. But it doesnt allow me to. It says there is some error related to access to the tables from the snapshots.
Any ideas?
Schema B needs to grant schema A select on the snapshots "WITH GRANT OPTION" ("WITH ADMIN OPTION" is only for system privileges, not object privileges):
grant select on TABLE_NAME to A with grant option;
Schema B needs to grant schema A select on the snapshots "with grant option" for schema A to be able to pass on the grants to other schemas/roles.
GRANT SELECT ON my_table TO a WITH GRANT OPTION;