Fine-grained access control privileges
This page describes the privileges that you can grant to a database role for fine-grained access control. This information applies to both GoogleSQL-dialect databases and PostgreSQL-dialect databases.
To learn about database roles and fine-grained access control, see Fine-grained access control overview.
The following table shows the fine-grained access control privileges and the database objects that they can be granted on.
| SELECT | INSERT | UPDATE | DELETE | EXECUTE | USAGE | |
|---|---|---|---|---|---|---|
| Schema | ✓ | |||||
| Table | ✓ | ✓ | ✓ | ✓ | ||
| Column | ✓ | ✓ | ✓ | ✓ | ||
| View | ✓ | |||||
| Change stream | ✓ | |||||
| Change stream read function | ✓ | |||||
| Sequence | ✓ | ✓ | ||||
| Model | ✓ |
The following sections provide details about each privilege.
SELECT
Allows the role to read or query from a table, view, change stream, sequence, or model.
If a column list is specified for a table, the privilege is valid on only those columns. If no column list is specified, then the privilege is valid on all columns in the table, including columns added afterward. A column list isn't allowed for a view.
Spanner supports both invoker's rights views and definer's rights views. For more information, see Views overview.
If you create a view with invoker's rights, to query the view, the database role or user needs the
SELECTprivilege on the view, and also theSELECTprivilege on the underlying objects referenced in the view. For example, suppose the viewSingerNamesis created on theSingerstable.CREATEVIEWSingerNamesSQLSECURITYINVOKERAS SELECTSingers.SingerId,Singers.FirstName,Singers.LastNameFROMSingers;Suppose that the database role
myRoleperforms the querySELECT * FROM SingerNames. The role must haveSELECTprivilege on the view and must haveSELECTprivilege on the three referenced columns or on the entireSingerstable.If you create a view with definer's rights, to query the view, the database role or user only needs the
SELECTprivilege on the view. For example, suppose the viewAlbumsBudgetis created on theAlbumstable.CREATEVIEWAlbumsBudgetSQLSECURITYDEFINERAS SELECTAlbums.Id,Albums.AlbumTitle,MarketingBudgetFROMAlbums;Suppose that the database role
Analystperforms the querySELECT * FROM AlbumsBudget. The role only needsSELECTprivilege on the view. It doesn't need theSELECTprivilege on the three referenced columns or on theAlbumstable.After granting
SELECTon a subset of columns for a table, the FGAC user can no longer useSELECT *on that table. Queries on that table must name all columns to be included.SELECTgranted on a generated column doesn't grantSELECTon the underlying base columns.For interleaved tables,
SELECTgranted on the parent table doesn't propagate to the child table.When you grant
SELECTon a change stream, you must also grantEXECUTEon the table-valued function for the change stream. For more information, see EXECUTE.When
SELECTis used with an aggregate function on specific columns, for exampleSUM(col_a), the role must have theSELECTprivilege on those columns. If the aggregate function doesn't specify any columns, for exampleCOUNT(*), the role must have theSELECTprivilege on at least one column in the table.When you use
SELECTwith a sequence, you can only view sequences that you have privileges to view.
Examples for using GRANT SELECT
GoogleSQL
GRANTSELECTONTABLEemployeesTOROLEhr_director; GRANTSELECTONTABLEcustomers,orders,itemsTOROLEaccount_mgr; GRANTSELECT(name,level,cost_center,location,manager)ONTABLEemployeesTOROLEhr_manager; GRANTSELECT(name,address,phone)ONTABLEemployees,contractorsTOROLEhr_rep; GRANTSELECTONVIEWorders_viewTOROLEhr_manager; GRANTSELECTONCHANGESTREAMordersChangeStreamTOROLEhr_analyst; GRANTSELECTONSEQUENCEsequence_nameTOROLErole_name;
PostgreSQL
GRANTSELECTONTABLEemployeesTOhr_director; GRANTSELECTONTABLEcustomers,orders,itemsTOaccount_mgr; GRANTSELECT(name,level,cost_center,location,manager)ONTABLEemployeesTOhr_manager; GRANTSELECT(name,address,phone)ONTABLEemployees,contractorsTOhr_rep; GRANTSELECTONTABLEorders_viewTOhr_manager;//orders_viewisaninvokerrightsview GRANTSELECTONCHANGESTREAMorders_change_streamTOhr_analyst; GRANTSELECTONSEQUENCEsequence_nameTOhr_package;
INSERT
Allows the role to insert rows into the specified tables. If a column list is specified, the permission is valid on only those columns. If no column list is specified, then the privilege is valid on all columns in the table.
If column names are specified, any column not included gets its default value upon insert.
INSERTcan't be granted on generated columns.
Examples for using GRANT INSERT
GoogleSQL
GRANTINSERTONTABLEemployees,contractorsTOROLEhr_manager; GRANTINSERT(name,address,phone)ONTABLEemployeesTOROLEhr_rep;
PostgreSQL
GRANTINSERTONTABLEemployees,contractorsTOhr_manager; GRANTINSERT(name,address,phone)ONTABLEemployeesTOhr_rep;
UPDATE
Allows the role to update rows in the specified tables. Updates can be
restricted to a subset of table columns. When you use this with sequences, it
allows the role to call the get-next-sequence-value function on the sequence.
In addition to the UPDATE privilege, the role needs the SELECT privilege on
all queried columns. Queried columns include columns in the WHERE clause.
UPDATE can't be granted on generated columns.
Examples for using GRANT UPDATE
GoogleSQL
GRANTUPDATEONTABLEemployees,contractorsTOROLEhr_manager; GRANTUPDATE(name,address,phone)ONTABLEemployeesTOROLEhr_rep;
PostgreSQL
GRANTUPDATEONTABLEemployees,contractorsTOhr_manager; GRANTUPDATE(name,address,phone)ONTABLEemployeesTOhr_rep;
DELETE
Allows the role to delete rows from the specified tables.
DELETEcan't be granted at the column level.The role also needs
SELECTon any columns that might be included in the query'sWHEREclauses.For interleaved tables in GoogleSQL-dialect databases, the
DELETEprivilege is required only on the parent table. If a child table specifiesON DELETE CASCADE, rows from the child table are deleted even without theDELETEprivilege on the child table.
Example for using GRANT DELETE
GoogleSQL
GRANTDELETEONTABLEemployees,contractorsTOROLEhr_admin;
PostgreSQL
GRANTDELETEONTABLEemployees,contractorsTOhr_admin;
EXECUTE
When you grant SELECT on a change stream, you must also grant EXECUTE on the
read function for the change stream. For more information, see
Change stream read functions and query syntax.
When you use this with models, it allows the role to use the model in machine learning functions.
Example for using GRANT EXECUTE
The following example shows how to grant EXECUTE on the read function for the
change stream named my_change_stream.
GoogleSQL
GRANTEXECUTEONTABLEFUNCTIONREAD_my_change_streamTOROLEhr_analyst;
PostgreSQL
GRANTEXECUTEONFUNCTIONspanner.read_json_my_change_streamTOhr_analyst;
USAGE
When you grant USAGE to a named schema, it provides privileges to access
objects contained in the named schema. The USAGE privilege is granted, by
default, to the default schema.
What's next
- Configure fine-grained access control
- Fine-grained access control overview
- GRANT and REVOKE statements (GoogleSQL-dialect databases)
- GRANT and REVOKE statements (PostgreSQL-dialect databases)