Sub topic 10
Granting and Revoking Privileges
This chapter assumes you can run SQL in a Databricks notebook or the SQL editor. It also assumes you know the three-level namespace (catalog.schema.table) and who owns an object. Here you will learn how to give a group access to data, check what you granted, and take it back.
The privilege model
Unity Catalog denies access by default. A principal (a user, group, or service principal) can use an object only if it owns the object or has been granted a privilege on it. Privileges are attached to securable objects: catalogs, schemas, tables, and others.
The statements are GRANT and REVOKE. A grant names three things: the privilege, the object, and the principal.
Reading a table needs three grants
To query a table, a principal needs SELECT on the table. It also needs USE CATALOG on the parent catalog and USE SCHEMA on the parent schema. Without the two USE privileges, SELECT alone has no effect, because the principal cannot reach the object through its parents.
GRANT USE CATALOG ON CATALOG dev TO `data-analysts`;
GRANT USE SCHEMA ON SCHEMA dev.sales TO `data-analysts`;
GRANT SELECT ON TABLE dev.sales.orders TO `data-analysts`;Backticks quote the group name because it contains a hyphen.
Granting at a higher level
Granting table by table gets tedious. If you grant SELECT on a schema, it applies to every table in that schema. This includes tables created later. You still need USE CATALOG on the catalog and USE SCHEMA on the schema.
GRANT SELECT ON SCHEMA dev.sales TO `data-analysts`;
SHOW GRANTS ON SCHEMA dev.sales;SHOW GRANTS ON lists each principal and the privileges it holds directly on that object. The next chapter explains how this inheritance works in detail.
Revoking privileges
REVOKE removes a privilege you granted earlier. The syntax mirrors GRANT, with FROM in place of TO.
REVOKE SELECT ON SCHEMA dev.sales FROM `data-analysts`;Revoking removes only the grant you name. In the example above, the group keeps the table-level SELECT on dev.sales.orders from the earlier grant. If you want the group to lose all access to that table, revoke that grant too.
Who can grant
Only certain principals can run GRANT on an object. These are the object's owner, a metastore admin, and a principal that has been given the right to manage privileges on it. Ownership was covered in the previous chapter. If a GRANT fails with a permission error, check who owns the object.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Group has `SELECT` but queries fail | Missing `USE CATALOG` or `USE SCHEMA` on a parent | Grant the missing `USE` privilege |
| Revoked a privilege but access remains | Another grant exists, on the table or on a group the user belongs to | Run `SHOW GRANTS` on each level and revoke the other grant |
| Syntax error on a group name | Name with a hyphen is not quoted | Wrap the name in backticks |
| `GRANT` is rejected | You are not the owner or an admin | Ask the owner, or have ownership transferred |
Exercise
Exercise
The group data-analysts has SELECT on the table dev.sales.orders and USE SCHEMA on dev.sales, but no other privileges. A member runs SELECT * FROM dev.sales.orders and gets a permission error. Which grant is missing, and what statement fixes it?
Show answer
USE CATALOG on the catalog dev is missing. Fix it with: GRANT USE CATALOG ON CATALOG dev TO data-analysts;
Exercise
You granted SELECT on the schema dev.sales to data-analysts and later ran REVOKE SELECT ON SCHEMA dev.sales FROM data-analysts. A member can still read dev.sales.orders. Give one possible reason and one command to investigate.
Show answer
The group may still hold a direct SELECT grant on the table itself (or the user may belong to another group with access). Run SHOW GRANTS ON TABLE dev.sales.orders; to see the remaining grants.