Sub topic 10
Granting and revoking privileges
This chapter assumes you can create catalogs, schemas, and tables, and that you know the difference between a user, a group, and a service principal. Unity Catalog controls access with privileges. A privilege is a permission on a securable object, such as SELECT on a table. You give privileges with GRANT and take them away with REVOKE. Inheritance and ownership have their own chapters, so this one covers only what you need to grant and revoke correctly.
The privilege model
Unity Catalog is deny by default. A principal can do nothing with an object until it has been given a privilege, or until it owns the object. A principal is a user, a group, or a service principal.
Every privilege is granted on a securable object. Securables form a hierarchy: metastore, catalog, schema, and then tables, views, volumes, and functions. Each grant has three parts: the privilege, the object, and the principal.
Common privileges:
| Privilege | Applies to | What it allows |
|---|---|---|
| `USE CATALOG` | Catalog | Look inside the catalog. Needed to reach anything in it. |
| `USE SCHEMA` | Schema | Look inside the schema. Needed to reach anything in it. |
| `SELECT` | Table, view | Read data. |
| `MODIFY` | Table | Insert, update, delete, and merge data. |
| `CREATE TABLE` | Schema | Create tables in the schema. |
| `CREATE SCHEMA` | Catalog | Create schemas in the catalog. |
| `ALL PRIVILEGES` | Any securable | Every privilege that applies to that object, except the ability to grant. |
The exact list changes between releases. Check the privilege list in the Databricks documentation for your workspace before you rely on a specific name.
Granting access to one table
Reading a table needs three privileges, not one. The principal needs USE CATALOG on the catalog, USE SCHEMA on the schema, and SELECT on the table. If any of the three is missing, the query fails.
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`;The USE privileges only let the group traverse the hierarchy. They do not expose any data by themselves. Names with a hyphen, such as data-analysts, must be wrapped in backticks. You can grant several privileges in one statement by separating them with commas, for example GRANT SELECT, MODIFY ON TABLE ....
Why the parent privileges matter
A common error is to grant SELECT on a table and then see a permission error for the user. The cause is almost always a missing USE CATALOG or USE SCHEMA. The following diagram shows the check that runs when a user reads dev.sales.orders.
Granting at a higher level
Granting table by table does not scale. You can grant a privilege on a schema instead. The privilege then applies to the tables in that schema, including tables created later. This is called inheritance, and a later chapter covers it in detail.
GRANT SELECT ON SCHEMA dev.sales TO `data-analysts`;
SHOW GRANTS ON SCHEMA dev.sales;SHOW GRANTS ON SCHEMA dev.sales lists the grants made directly on that schema. It shows the principal, the privilege, and the object type. It does not list grants that the schema inherits from its catalog. To see those, run SHOW GRANTS on the catalog.
Revoking privileges
REVOKE removes a privilege from a principal. 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. This leads to two behaviors that surprise people.
- If the group also has
SELECTon a specific table in that schema, that table grant stays. The group can still read the table. - If the user belongs to another group that has
SELECT, the user keeps access through that group.
When a user still has access after a revoke, list the grants on every level above the object and check the user's group memberships. One of them is the source.
Who can grant
Not everyone can run GRANT. You can grant a privilege on an object if you are one of these:
- The owner of the object.
- The owner of a parent object, such as the catalog that contains the schema.
- A principal that holds the
MANAGEprivilege on the object. - A metastore admin.
Ownership is an implicit set of privileges. The owner of a table can read it, change it, and grant access to it, without any explicit grants. Creating an object makes you its owner by default.
Granting write access
Read-only access covers analysts. Pipelines and engineers need to write. A typical set of grants for an engineering group looks like this:
GRANT USE CATALOG ON CATALOG dev TO `data-engineers`;
GRANT USE SCHEMA ON SCHEMA dev.sales TO `data-engineers`;
GRANT SELECT, MODIFY ON SCHEMA dev.sales TO `data-engineers`;
GRANT CREATE TABLE ON SCHEMA dev.sales TO `data-engineers`;MODIFY lets the group change data in existing tables. CREATE TABLE is a separate privilege, because creating a table changes the schema's contents and not a table's data. A group that can create a table in a schema becomes the owner of that new table.
Granting to the right principal
Unity Catalog accepts three kinds of principal in a grant.
| Principal | How you write it |
|---|---|
| User | The email address in backticks, such as `` `[email protected]` `` |
| Group | The group name in backticks, such as `` `data-analysts` `` |
| Service principal | The application ID in backticks |
There is also a built-in group named account users. It contains every user in the Databricks account. Granting to it gives access to everyone, so use it only for data that is meant to be broadly visible.
If you grant to a name that does not exist, the statement fails with an error. A misspelled group name is therefore caught at grant time. A group that exists in the account but has no members is not caught, so a grant can succeed and still help nobody.
Checking what a principal can do
Use SHOW GRANTS to inspect access. Two forms are common.
SHOW GRANTS ON TABLE dev.sales.orders;
SHOW GRANTS `data-analysts` ON SCHEMA dev.sales;The first lists every principal with a direct grant on the table. The second lists the privileges one principal holds on one object. Use the second form when a user reports a problem, and run it on the table, the schema, and the catalog.
Common errors
- Table grant without parent grants.
SELECTon a table alone is not enough. AddUSE CATALOGandUSE SCHEMA. - Granting to a user instead of a group. The grant works, but it is hard to audit and to remove when the person changes role.
- Expecting
REVOKEto deny. A revoke removes a grant. It does not block access that arrives from another grant, another group, or ownership. - Missing backticks. A name with a hyphen or an
@must be quoted, or the statement fails to parse. - Running
GRANTwithout authority. If you are not an owner, aMANAGEholder, or a metastore admin, the statement fails with a permission error.
Exercise
A user in the group data-analysts has SELECT on dev.sales.orders and USE SCHEMA on dev.sales, but the query fails with a permission error. Which grant is most likely missing, and which statement fixes it?
Show answer
USE CATALOG on the catalog dev is missing. Run GRANT USE CATALOG ON CATALOG dev TO data-analysts;. Reading a table needs USE CATALOG, USE SCHEMA, and SELECT.
Exercise
You run REVOKE SELECT ON SCHEMA dev.sales FROM data-analysts;, but a member of that group can still read dev.sales.orders. Name two possible reasons and a command that helps you find them.
Show answer
The group may hold a separate SELECT grant directly on the table, or the user may get SELECT through another group. Run SHOW GRANTS ON TABLE dev.sales.orders; and check the user's group memberships.