GRANT
Grants privileges, roles, and ownership for a specific database object. This includes:
- Granting privileges to users or roles.
- Assigning roles to users or other roles.
- Transferring ownership to a role.
See also:
Syntax
Granting Privileges
To understand what a privilege is and how it works, see Privileges.
GRANT {
schemaObjectPrivileges | ALL [ PRIVILEGES ] ON <privileges_level>
}
TO [ ROLE <role_name> ] [ <user_name> ]
Where:
schemaObjectPrivileges ::=
-- For TABLE
{ SELECT | INSERT }
-- For SCHEMA
{ CREATE | DROP | ALTER }
-- For USER
{ CREATE USER }
-- For ROLE
{ CREATE ROLE}
-- For STAGE
{ READ, WRITE }
-- For UDF
{ USAGE }
-- For MASKING POLICY (account-level privileges)
{ CREATE MASKING POLICY | APPLY MASKING POLICY }
privileges_level ::=
*.*
| db_name.*
| db_name.tbl_name
| STAGE <stage_name>
| UDF <udf_name>
| MASKING POLICY <policy_name>
Granting Masking Policy Privileges
Use the following forms to manage access to individual masking policies:
GRANT APPLY ON MASKING POLICY <policy_name> TO [ ROLE ] <grantee>
GRANT ALL [ PRIVILEGES ] ON MASKING POLICY <policy_name> TO [ ROLE ] <grantee>
GRANT OWNERSHIP ON MASKING POLICY <policy_name> TO ROLE '<role_name>'
CREATE MASKING POLICYallows a user or role to create new masking policies.APPLY MASKING POLICYlets grantees attach, detach, describe, or drop any masking policy when combined with the appropriateALTER TABLEor policy commands.GRANT APPLY ON MASKING POLICY ...authorizes the grantee to manage a specific masking policy without granting global access.- OWNERSHIP provides full control over the masking policy; Databend automatically grants OWNERSHIP on a new policy to the creator role and revokes it when the policy is dropped.
Granting Role
To understand what a role is and how it works, see Roles.
-- Grant a role to a user
GRANT ROLE <role_name> TO <user_name>
-- Grant a role to a role
GRANT ROLE <role_name> TO ROLE <role_name>
Granting Ownership
To understand what ownership is and how it works, see Ownership.
-- Grant ownership of a specific table within a database to a role
GRANT OWNERSHIP ON <database_name>.<table_name> TO ROLE '<role_name>'
-- Grant ownership of a stage to a role
GRANT OWNERSHIP ON STAGE <stage_name> TO ROLE '<role_name>'
-- Grant ownership of a user-defined function (UDF) to a role
GRANT OWNERSHIP ON UDF <udf_name> TO ROLE '<role_name>'
Examples
Example 1: Granting Privileges to a User
Create a user:
CREATE USER user1 IDENTIFIED BY 'abc123';
Grant the ALL privilege on all existing tables in the default database to the user user1:
GRANT ALL ON default.* TO user1;
SHOW GRANTS FOR user1;
+-----------------------------------------+
| Grants |
+-----------------------------------------+
| GRANT ALL ON 'default'.* TO 'user1'@'%' |
+-----------------------------------------+
Grant the ALL privilege to all the database to the user user1:
GRANT ALL ON *.* TO 'user1';
SHOW GRANTS FOR user1;
+-----------------------------------------+
| Grants |
+-----------------------------------------+
| GRANT ALL ON 'default'.* TO 'user1'@'%' |
| GRANT ALL ON *.* TO 'user1'@'%' |
+-----------------------------------------+
Grant the ALL privilege to the stage that named s1 to the user user1:
GRANT ALL ON STAGE s1 TO 'user1';
SHOW GRANTS FOR user1;
+-----------------------------------------------------------------+
| Grants |
+-----------------------------------------------------------------+
| GRANT ALL ON STAGE s1 TO 'user1'@'%' |
| GRANT SELECT ON 'default'.'system'.'one' TO 'user1'@'%' |
| GRANT SELECT ON 'default'.'information_schema'.* TO 'user1'@'%' |
+-----------------------------------------------------------------+
Grant the ALL privilege to the UDF that named f1 to the user user1:
GRANT ALL ON UDF f1 TO 'user1';
SHOW GRANTS FOR user1;
+-----------------------------------------------------------------+
| Grants |
+-----------------------------------------------------------------+
| GRANT ALL ON UDF f1 TO 'user1'@'%' |
| GRANT SELECT ON 'default'.'system'.'one' TO 'user1'@'%' |
| GRANT SELECT ON 'default'.'information_schema'.* TO 'user1'@'%' |
+-----------------------------------------------------------------+
Example 2: Granting Privileges to a Role
Grant the SELECT privilege on all existing tables in the mydb database to the role role1:
Create role:
CREATE ROLE role1;
Grant privileges to the role:
GRANT SELECT ON mydb.* TO ROLE role1;
Show the grants for the role:
SHOW GRANTS FOR ROLE role1;
+-------------------------------------+
| Grants |
+-------------------------------------+
| GRANT SELECT ON 'mydb'.* TO 'role1' |
+-------------------------------------+
Example 3: Granting a Role to a User
User user1 grants are:
SHOW GRANTS FOR user1;
+-----------------------------------------+
| Grants |
+-----------------------------------------+
| GRANT ALL ON 'default'.* TO 'user1'@'%' |
| GRANT ALL ON *.* TO 'user1'@'%' |
+-----------------------------------------+
Role role1 grants are:
SHOW GRANTS FOR ROLE role1;
+-------------------------------------+
| Grants |
+-------------------------------------+
| GRANT SELECT ON 'mydb'.* TO 'role1' |
+-------------------------------------+
Grant role role1 to user user1:
GRANT ROLE role1 TO user1;
Now, user user1 grants are:
SHOW GRANTS FOR user1;
+-----------------------------------------+
| Grants |
+-----------------------------------------+
| GRANT ALL ON 'default'.* TO 'user1'@'%' |
| GRANT ALL ON *.* TO 'user1'@'%' |
| GRANT SELECT ON 'mydb'.* TO 'role1' |
+-----------------------------------------+
Example 4: Granting Ownership to a Role
-- Grant ownership of all tables in the 'finance_data' database to the role 'data_owner'
GRANT OWNERSHIP ON finance_data.* TO ROLE 'data_owner';
-- Grant ownership of the table 'transactions' in the 'finance_data' schema to the role 'data_owner'
GRANT OWNERSHIP ON finance_data.transactions TO ROLE 'data_owner';
-- Grant ownership of the stage 'ingestion_stage' to the role 'data_owner'
GRANT OWNERSHIP ON STAGE ingestion_stage TO ROLE 'data_owner';
-- Grant ownership of the user-defined function 'calculate_profit' to the role 'data_owner'
GRANT OWNERSHIP ON UDF calculate_profit TO ROLE 'data_owner';
Example 5: Granting Masking Policy Privileges
-- Allow the current user to create masking policies
GRANT CREATE MASKING POLICY ON *.* TO ROLE security_admin;
-- Create a masking policy while assuming the security_admin role
CREATE MASKING POLICY email_mask AS (val STRING) RETURNS STRING -> '***';
-- Grant a role the ability to apply the policy when altering tables
GRANT APPLY ON MASKING POLICY email_mask TO ROLE pii_readers;
-- Review the masking policy privileges
SHOW GRANTS ON MASKING POLICY email_mask;