Skip to main content

Privileges

A privilege is a permission to perform an action. Users must have specific privileges to execute particular actions within Databend. For example, when querying a table, a user needs SELECT privileges to the table. Similarly, to read a dataset within a stage, the user must possess READ privileges.

In Databend, users can obtain a privilege in two ways. One approach is to directly grant the privilege to the user. The other method involves granting the privilege to a role first, and then assigning that role to the user.

Alt text

Managing Privileges

To manage privileges for a user or a role, use the following commands:

Granting Privileges to User / Role

To grant a privilege, you have two options: you can either directly grant the privilege to a user, or you can grant the privilege to a role first, and then grant that role to the user. In the following example, privileges are directly granted to the user 'david'. 'david' is created as a new user with the password 'abc123', and then all privileges on objects in the 'default' schema are granted directly to 'david'. Finally, the granted privileges for 'david' are shown.

Example-1:
-- Create a new user named 'david' with the password 'abc123'
CREATE USER david IDENTIFIED BY 'abc123';

-- Grant all privileges on all objects in the 'default' schema to the user 'david'
GRANT ALL ON default.* TO david;

-- Show the granted privileges for the user 'david'
SHOW GRANTS FOR david;

┌───────────────────────────────────────────────────┐
│ Grants │
├───────────────────────────────────────────────────┤
GRANT ALL ON 'default'.'default'.* TO 'david'@'%'
└───────────────────────────────────────────────────┘

In the following example, privileges are granted to a role first, and then the role is granted to the user 'eric'. Initially, a new role named 'writer' is created and granted all privileges on objects in the 'default' schema. Subsequently, 'eric' is created as a new user with the password 'abc123', and the 'writer' role is granted to 'eric'. Finally, the granted privileges for 'eric' are shown.

Example-2:
-- Create a new role named 'writer'
CREATE ROLE writer;

-- Grant all privileges on all objects in the 'default' schema to the role 'writer'
GRANT ALL ON default.* TO ROLE writer;

-- Create a new user named 'eric' with the password 'abc123'
CREATE USER eric IDENTIFIED BY 'abc123';

-- Grant the role 'writer' to the user 'eric'
GRANT ROLE writer TO eric;

-- Show the granted privileges for the user 'eric'
SHOW GRANTS FOR eric;

┌──────────────────────────────────────────────────┐
│ Grants │
├──────────────────────────────────────────────────┤
GRANT ALL ON 'default'.'default'.* TO 'eric'@'%'
└──────────────────────────────────────────────────┘

Revoking Privileges from User / Role

In the context of access control, privileges can be revoked either from individual users or from roles. In the following example, we revoke all privileges on all objects in the 'default' schema from user 'david', and then we display the granted privileges for user 'david':

Example-1(Continued):
-- Revoke all privileges on all objects in the 'default' schema from user 'david'
REVOKE ALL ON default.* FROM david;

-- Show the granted privileges for the user 'david'
SHOW GRANTS FOR david;

In the following example, privileges are revoked for role 'writer' on all objects in the 'default' schema. Following this, the granted privileges for user 'eric' are displayed.

Example-2(Continued):
-- Revoke all privileges on all objects in the 'default' schema from role 'writer'
REVOKE ALL ON default.* FROM ROLE writer;

-- Show the granted privileges for the user 'eric'
-- No privileges are displayed as they have been revoked from the role
SHOW GRANTS FOR eric;

Access Control Privileges

Databend offers a range of privileges that allow you to exercise fine-grained control over your database objects. Databend privileges can be categorized into the following types:

All Privileges

PrivilegeObject TypeDescription
ALLAllGrants all the privileges for the specified object type.
ALTERGlobal, Database, Table, ViewAlters a database, table, user or UDF.
CREATEGlobal, Database, TableCreates a database, table or UDF.
DELETETableDeletes or truncates rows in a table.
DROPGlobal, Database, Table, ViewDrops a database, table, view or UDF. Undrops a table.
INSERTTableInserts rows into a table.
SELECTDatabase, TableSelects rows from a table. Shows or uses a database.
UPDATETableUpdates rows in a table.
GRANTGlobalGrants / revokes privileges to / from a user or role.
SUPERGlobal, TableKills a query. Sets global configs. Optimizes a table. Analyzes a table. Operates a stage(Lists stages. Creates, Drops a stage), catalog or share.
USAGEGlobalSynonym for “no privileges”.
CREATE ROLEGlobalCreates a role.
DROP ROLEGlobalDrops a role.
CREATE USERGlobalCreates a SQL user.
DROP USERGlobalDrops a SQL user.
WRITEStageWrite into a stage.
READStageRead a stage.
USAGEUDFUse udf.

Global Privileges

PrivilegeDescription
ALLGrants all the privileges for the specified object type.
ALTERAdds or drops a table column. Alters a cluster key. Re-clusters a table.
CREATEROLECreates a role.
DROPUSERDrops a user.
CREATEUSERCreates a user.
DROPROLEDrops a role.
SUPERKills a query. Sets or unsets a setting. Operates a stage, catalog or share. Calls a function. COPY INTO a stage.
USAGEConnects to a databend query only.
CREATECreates a UDF.
DROPDrops a UDF.
ALTERAlters a UDF. Alters a SQL user.

Table Privileges

PrivilegeDescription
ALLGrants all the privileges for the specified object type.
ALTERAdds or drops a table column. Alters a cluster key. Re-clusters a table.
CREATECreates a table.
DELETEDeletes rows in a table. Truncates a table.
DROPDrops or undrops a table. Restores the recent version of a dropped table.
INSERTInserts rows into a table. COPY INTO a table.
SELECTSelects rows from a table. SHOW CREATE a table. DESCRIBE a table.
UPDATEUpdates rows in a table.
SUPEROptimizes or analyzes a table.
OWNERSHIPGrants full control over a database. Only a single role can hold this privilege on a specific object at a time.

View Privileges

PrivilegeDescription
ALLGrants all the privileges for the specified object type
ALTERCreates or drops a view. Alters the existing view using another QUERY.
DROPDrops a view.

Database Privileges

Please note that you can use the USE DATABASE command to specify a database once you have any of the following privileges to the database or any privilege to a table in the database.

PrivilegeDescription
AlterRenames a database.
CREATECreates a database.
DROPDrops or undrops a database. Restores the recent version of a dropped database.
SELECTSHOW CREATE a database.
OWNERSHIPGrants full control over a database. Only a single role can hold this privilege on a specific object at a time.

Note:

  1. If a role own a database, this role can access all tables under this database.

Session Policy Privileges

PrivilegeDescription
SUPERKills a query. Sets or unsets a setting.
ALLGrants all the privileges for the specified object type.

Stage Privileges

PrivilegeDescription
WRITEWrite into a stage. For example, copy into a stage, presign upload or removes a stage
READRead a stage. For example, list stage, query stage, copy into table from stage, presign download
ALLGrants READ, WRITE privileges for the specified object type.
OWNERSHIPGrants full control over a stage. Only a single role can hold this privilege on a specific object at a time.

Note:

  1. Don't check external location auth.

UDF Privileges

PrivilegeDescription
USAGECan use UDF. For example, copy into a stage, presign upload
ALLGrants READ, WRITE privileges for the specified object type.
OWNERSHIPGrants full control over a UDF. Only a single role can hold this privilege on a specific object at a time.

Note:

  1. Don't check the udf auth if it's already be constantly folded.
  2. Don't check the udf auth if it's a value in insert.

Catalog Privileges

PrivilegeDescription
SUPERSHOW CREATE catalog. Creates or drops a catalog.
ALLGrants all the privileges for the specified object type.