Sub topic 5
Granting, revoking and inspecting privileges
This chapter covers the SQL commands that change and read access in Unity Catalog: GRANT, REVOKE and SHOW GRANTS. It assumes you know basic SQL, can run statements in a notebook or the SQL editor, and understand the securable hierarchy (metastore, catalog, schema, table) and the idea of principals and groups from earlier chapters.
The model in one sentence
A privilege is a permission on a securable object. You give it to a principal with GRANT, take it away with REVOKE, and look at the current state with SHOW GRANTS. Unity Catalog is deny-by-default. If no grant applies to a principal, the principal gets no access.
Privileges are additive. There is no "deny" privilege, so the effective access of a principal is the union of what was granted to it directly, to every group it belongs to, and on parent objects through inheritance.
The GRANT statement
The general form is:
GRANT <privilege> [, <privilege> ...]
ON <securable_type> <securable_name>
TO <principal>;The principal is a user (identified by email), a service principal (identified by its application ID) or a group (identified by name). Wrap names that contain special characters, such as hyphens or @, in backticks.
The three grants needed to read a table
Reading a table requires privileges on the table and on the objects above it. SELECT on the table alone is not enough.
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`;USE CATALOGlets the principal resolve names inside the catalog.USE SCHEMAlets the principal resolve names inside the schema.SELECTlets the principal read the table.
The most common cause of a "table or view not found" or "permission denied" error is a missing USE CATALOG or USE SCHEMA. If a user has SELECT on a table but cannot query it, check the parents first.
Granting several privileges at once
You can list several privileges in one statement. ALL PRIVILEGES is a shortcut for every privilege that applies to the object type, but it does not include MANAGE-level control of grants beyond what the type supports, and it is rarely the right choice for a least-privilege design.
GRANT SELECT, MODIFY ON TABLE main.sales.orders TO `data-engineers`;MODIFY allows INSERT, UPDATE, DELETE and MERGE on a table's data. SELECT is listed separately because MODIFY does not imply read access.
Granting to the right kind of principal
Prefer granting to groups. When a person joins or leaves a team, you change group membership once, and no per-table grants need editing. Grants to individual users accumulate and are hard to review.
| Principal type | How to write it | Typical use |
|---|---|---|
| Group | `` `data-analysts` `` | Human teams and roles |
| User | `` `[email protected]` `` | Rare, short-term exceptions |
| Service principal | `` `1b2c3d4e-0000-0000-0000-abcdef123456` `` | Jobs and automation |
Who is allowed to grant
Not everyone can run GRANT. You can grant a privilege on an object if you are one of the following:
- The owner of the object.
- A principal with the
MANAGEprivilege on the object. - The owner of a parent object (the schema owner can grant on tables in the schema, for example).
- A metastore admin, for objects in the metastore.
A principal who holds SELECT does not gain the right to hand SELECT to others. Unity Catalog does not have a "with grant option" model. Delegation is done through ownership and MANAGE, which a later chapter covers.
The REVOKE statement
REVOKE removes a privilege that was granted at the same level and to the same principal.
GRANT SELECT ON SCHEMA main.sales TO `data-analysts`;
REVOKE SELECT ON SCHEMA main.sales FROM `data-analysts`;Note that the grant on a schema is inherited by every current and future table and view in that schema. The REVOKE above removes it at the same level, so the inherited access disappears as well.
Why a revoke sometimes appears to do nothing
REVOKE only removes the specific grant you name. Access can persist for one of these reasons:
- The grant was made at a different level. If
SELECTwas granted on the catalog and you revoke it on a table, nothing changes, because the table-level grant never existed. Revoke at the level where the grant lives. - The principal gets the privilege through another group. Revoking from
[email protected]has no effect if Ana belongs todata-analysts, which still holds the grant. - The principal owns the object, or is an admin. Owners and admins do not rely on grants for the access their role gives them.
When a user still has access after a revoke, do not guess. Inspect the grants, as shown next.
Inspecting privileges with SHOW GRANTS
SHOW GRANTS has two useful shapes. The first lists every grant on an object. The second lists what one principal holds on that object.
SHOW GRANTS ON TABLE main.sales.orders;
SHOW GRANTS `data-analysts` ON SCHEMA main.sales;The result has these columns:
| Column | Meaning |
|---|---|
| `principal` | The user, service principal or group that holds the privilege |
| `actionType` | The privilege, such as `SELECT` or `USE SCHEMA` |
| `objectType` | The type of object the grant is on |
| `objectKey` | The full name of that object |
An important detail: depending on your Databricks release, SHOW GRANTS ON <object> may show only grants made directly on that object, and may also include inherited grants with the object they were inherited from in objectKey. Check the objectKey column rather than assuming a row belongs to the object you asked about.
To find the full picture for a principal, run SHOW GRANTS on each level of the path: catalog, schema, then table. Then repeat the check for each group the principal belongs to.
A worked diagnosis
Suppose Ana, a member of data-analysts, gets a permission error on main.sales.orders. Work from the top down.
SHOW GRANTS `data-analysts` ON CATALOG main;
SHOW GRANTS `data-analysts` ON SCHEMA main.sales;
SHOW GRANTS `data-analysts` ON TABLE main.sales.orders;You expect to see USE CATALOG, USE SCHEMA and SELECT respectively. If the first query returns no rows, USE CATALOG is missing and a grant on the catalog fixes the problem. If the table query returns no rows but the schema has SELECT, the schema grant should cover the table through inheritance, so look for a different cause, such as a row filter or an unexpected group name.
Privileges on other object types
The same commands work on every securable object. Only the privilege names and the keyword for the object type change.
GRANT USE CATALOG ON CATALOG main TO `data-engineers`;
GRANT CREATE TABLE ON SCHEMA main.sales TO `data-engineers`;
GRANT READ VOLUME ON VOLUME main.sales.raw_files TO `data-engineers`;
GRANT EXECUTE ON FUNCTION main.sales.us_only TO `data-analysts`;If a statement fails with a message that a privilege is not valid for the object type, you have named a privilege that does not belong to that type. For example, SELECT applies to tables and views but READ VOLUME applies to volumes.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| User has `SELECT` but the query fails | Missing `USE CATALOG` or `USE SCHEMA` | Grant the parent privileges |
| `GRANT` fails with a permission error | You are not the owner and lack `MANAGE` | Ask the owner or a metastore admin |
| Principal not found | Name or email misspelled, or group not synced to the account | Check spelling and that the group exists at the account level |
| `REVOKE` succeeds but access remains | Grant exists at another level or via another group | Run `SHOW GRANTS` at each level and for each group |
| Name with a hyphen causes a syntax error | Missing backticks | Wrap the principal in backticks |
Good habits
- Grant to groups, not individuals.
- Grant at the schema level for shared access, and at the table level only for exceptions.
- Grant the smallest set of privileges that does the job. Start with
SELECTand addMODIFYonly for principals that write. - Run
SHOW GRANTSafter a change to confirm the result, and keep the statements in version control or a deployment script so access can be reproduced and reviewed.
Exercise
A group finance-readers needs to query main.reports.monthly and nothing else. Write the statements that give it that access. Then explain why you revoke SELECT on main.reports.monthly from the group but a user in the group can still query the table.
Show answer
GRANT USE CATALOG ON CATALOG main TO finance-readers; GRANT USE SCHEMA ON SCHEMA main.reports TO finance-readers; GRANT SELECT ON TABLE main.reports.monthly TO finance-readers; After the revoke, the user can still query the table if the access comes from another source: the user belongs to a second group that holds SELECT, the user holds a direct grant, a SELECT grant exists on the schema or catalog and is inherited, or the user owns the table or is an admin. Run SHOW GRANTS on the table, schema and catalog, and for each of the user's groups, to find the remaining source.
Exercise
Ana has SELECT on main.sales.orders but gets an error when she runs SELECT * FROM main.sales.orders. Which privileges do you check first, and in what order?
Show answer
Check USE CATALOG on main, then USE SCHEMA on main.sales, since SELECT on the table is useless without them. Use SHOW GRANTS for Ana and for each of her groups at each level. If all three are present, look at row filters, column masks and the compute access mode.