Sub topic 5
Granting, Revoking and Inspecting Privileges
This chapter assumes you know basic SQL, can use the Databricks SQL editor or a notebook, and understand the Unity Catalog hierarchy of metastore, catalog, schema and table. It also assumes you know what a privilege type is. Here you will use GRANT, REVOKE and SHOW GRANTS to manage access.
The GRANT statement
A privilege is a permission on a securable object. You give it to a principal, which is a user, service principal or group, with GRANT. The general form is:
GRANT <privilege> ON <object type> <object name> TO <principal>
Principals that contain special characters, such as hyphens, must be wrapped in backticks. Prefer groups over individual users so that access follows team membership.
To query a table, a principal needs three privileges. It needs USE CATALOG on the catalog, USE SCHEMA on the schema and SELECT on the table.
GRANT USE CATALOG ON CATALOG main TO `data-analysts`;
GRANT USE SCHEMA ON SCHEMA main.sales TO `data-analysts`;
GRANT SELECT ON TABLE main.sales.orders TO `data-analysts`;Inspecting privileges with SHOW GRANTS
SHOW GRANTS lists the privileges on an object. Add a principal name to see only what that principal holds on the object.
SHOW GRANTS ON TABLE main.sales.orders;
SHOW GRANTS `data-analysts` ON SCHEMA main.sales;The output has one row per grant. The columns show the principal, the object type, the object name and the action type, which is the privilege.
SHOW GRANTS on an object shows grants made directly on that object. Depending on your workspace, it may also show privileges inherited from parent objects. Check the output columns before you conclude that a principal has no access.
Revoking privileges
REVOKE removes a privilege that was granted. It works at the same level as the grant. Privileges granted on a schema are inherited by the tables and views in it, so revoking at the schema level removes that inherited access too.
GRANT SELECT ON SCHEMA main.sales TO `data-analysts`;
REVOKE SELECT ON SCHEMA main.sales FROM `data-analysts`;Note the keyword change: you GRANT ... TO a principal and REVOKE ... FROM a principal.
A revoke only removes the privilege you name, at the level you name. If you granted SELECT on a table directly, revoking SELECT on the schema does not remove it. Run SHOW GRANTS on the table afterwards to confirm the result.
Who can grant
Not everyone can run GRANT. You can grant a privilege on an object if you own it, or if you hold the MANAGE privilege on it. Metastore admins can also grant on objects in the metastore. If a GRANT fails with a permission error, check ownership first.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Query fails after granting SELECT | Missing USE CATALOG or USE SCHEMA | Grant both on the parent objects |
| Syntax error on a group name | Name with a hyphen is not quoted | Wrap the name in backticks |
| Revoke has no visible effect | Access also comes from a direct grant or another group | Run SHOW GRANTS at each level and for each group the user belongs to |
| GRANT fails with a permission error | You are not the owner and lack MANAGE | Ask the owner or an admin to grant, or to give you MANAGE |
Exercise
A user in the group data-analysts has SELECT on main.sales.orders but still cannot query it. Which two privileges should you check, and which statement shows what the group holds on the schema?
Show answer
Check USE CATALOG on the catalog main and USE SCHEMA on main.sales. Run SHOW GRANTS data-analysts ON SCHEMA main.sales; to see what the group holds on the schema, and run the same statement with ON CATALOG main for the catalog.
Exercise
Write the statement that removes SELECT on the schema main.sales from the group data-analysts.
Show answer
REVOKE SELECT ON SCHEMA main.sales FROM data-analysts;