ALTER DEFAULT PRIVILEGES

On this page Carat arrow pointing down

The ALTER DEFAULT PRIVILEGES statement changes the default privileges on objects created by users/roles in the current database.

Note:

The creator of an object is also the object's owner. Any roles that are members of the owner role have ALL privileges on the object. Altering the default privileges of objects created by a role does not affect that role's privileges as the object's owner. The default privileges granted to other users/roles are always in addition to the ownership (i.e., ALL) privileges given to the creator of the object.

Note:

If you grant a default privilege to a user/role for all objects created by a specific user/role, neither of the users/roles can be dropped until the default privilege is revoked.

For an example, see Grant default privileges to a specific role.

Synopsis

ALTER DEFAULT PRIVILEGES FOR ROLE USER role_spec_list ALL ROLES IN SCHEMA qualifiable_schema_name , GRANT privileges ON TABLES SEQUENCES TYPES SCHEMAS TO role_spec_list WITH GRANT OPTION REVOKE GRANT OPTION FOR privileges ON TABLES SEQUENCES TYPES SCHEMAS FROM role_spec_list CASCADE RESTRICT

Parameters

Parameter Description
FOR ROLE name/FOR USER name Alter the default privileges on objects created by a specific role/user, or a list of roles/users.
FOR ALL ROLES Alter the default privileges on objects created by all users/roles.
GRANT ... Grant a default privilege or list of privileges on all objects of the specified type to a role/user, or a list of roles/users.
REVOKE ... Revoke a default privilege or list of privileges on all objects of the specified type from a role/user, or a list of roles/users.
IN SCHEMA qualifiable_schema_name New in v22.1: If specified, the default privileges are altered for objects created in that schema. If an object has default privileges specified at the database and at the schema level, the union of the default privileges is taken.
Note:

If you do not specify a FOR ... clause, CockroachDB alters the default privileges on objects created by the current user.

Required privileges

  • To run ALTER DEFAULT PRIVILEGES FOR ALL ROLES, the user must be a member of the admin role.
  • To alter the default privileges on objects created by a specific role, the user must be a member of that role.

Examples

Grant default privileges to a specific role

Run the following statements as a member of the admin role, with ALL privileges:

icon/buttons/copy
> CREATE ROLE cockroachlabs WITH LOGIN;
icon/buttons/copy
> GRANT CREATE ON DATABASE defaultdb TO cockroachlabs;
icon/buttons/copy
> CREATE USER max WITH LOGIN;
icon/buttons/copy
> SHOW DEFAULT PRIVILEGES FOR ROLE cockroachlabs;
      role      | for_all_roles | object_type |    grantee    | privilege_type
----------------+---------------+-------------+---------------+-----------------
  cockroachlabs |     false     | schemas     | cockroachlabs | ALL
  cockroachlabs |     false     | sequences   | cockroachlabs | ALL
  cockroachlabs |     false     | tables      | cockroachlabs | ALL
  cockroachlabs |     false     | types       | cockroachlabs | ALL
  cockroachlabs |     false     | types       | public        | USAGE
(5 rows)

In the same database, run the following statements as the cockroachlabs user:

icon/buttons/copy
> ALTER DEFAULT PRIVILEGES FOR ROLE cockroachlabs GRANT SELECT ON TABLES TO max;
Note:

Because cockroachlabs is the current user, the previous statement is equivalent to ALTER DEFAULT PRIVILEGES GRANT SELECT ON TABLES TO max;.

icon/buttons/copy
> SHOW DEFAULT PRIVILEGES;
      role      | for_all_roles | object_type |    grantee    | privilege_type
----------------+---------------+-------------+---------------+-----------------
  cockroachlabs |     false     | schemas     | cockroachlabs | ALL
  cockroachlabs |     false     | sequences   | cockroachlabs | ALL
  cockroachlabs |     false     | tables      | cockroachlabs | ALL
  cockroachlabs |     false     | tables      | max           | SELECT
  cockroachlabs |     false     | types       | cockroachlabs | ALL
  cockroachlabs |     false     | types       | public        | USAGE
(6 rows)
icon/buttons/copy
> CREATE TABLE albums (
        id UUID PRIMARY KEY,
        title STRING,
        length DECIMAL,
        tracklist JSONB
);

In the same database, run the following statements as the max user:

icon/buttons/copy
> DROP TABLE albums;
ERROR: user max does not have DROP privilege on relation albums
SQLSTATE: 42501
icon/buttons/copy
> SHOW COLUMNS FROM albums;
  column_name | data_type | is_nullable | column_default | generation_expression |  indices  | is_hidden
--------------+-----------+-------------+----------------+-----------------------+-----------+------------
  id          | UUID      |    false    | NULL           |                       | {primary} |   false
  title       | STRING    |    true     | NULL           |                       | {primary} |   false
  length      | DECIMAL   |    true     | NULL           |                       | {primary} |   false
  tracklist   | JSONB     |    true     | NULL           |                       | {primary} |   false
(4 rows)

Because max has default SELECT privileges on all tables created by cockroachlabs, neither user/role can be dropped until all privileges are revoked.

To see this, run the following statements as a member of the admin role, with ALL privileges:

icon/buttons/copy
> DROP USER max;
ERROR: cannot drop role/user max: grants still exist on defaultdb.public.albums
SQLSTATE: 2BP01
icon/buttons/copy
> DROP USER cockroachlabs;
ERROR: cannot drop role/user cockroachlabs: grants still exist on defaultdb, defaultdb.public.albums
SQLSTATE: 2BP01

Revoke default privileges from a specific role

Run the following statements as the cockroachlabs user:

icon/buttons/copy
> SHOW DEFAULT PRIVILEGES;
      role      | for_all_roles | object_type |    grantee    | privilege_type
----------------+---------------+-------------+---------------+-----------------
  cockroachlabs |     false     | schemas     | cockroachlabs | ALL
  cockroachlabs |     false     | sequences   | cockroachlabs | ALL
  cockroachlabs |     false     | tables      | cockroachlabs | ALL
  cockroachlabs |     false     | tables      | max           | SELECT
  cockroachlabs |     false     | types       | cockroachlabs | ALL
  cockroachlabs |     false     | types       | public        | USAGE
(6 rows)
icon/buttons/copy
> ALTER DEFAULT PRIVILEGES FOR ROLE cockroachlabs REVOKE SELECT ON TABLES FROM max;
icon/buttons/copy
> SHOW DEFAULT PRIVILEGES;
      role      | for_all_roles | object_type |    grantee    | privilege_type
----------------+---------------+-------------+---------------+-----------------
  cockroachlabs |     false     | schemas     | cockroachlabs | ALL
  cockroachlabs |     false     | sequences   | cockroachlabs | ALL
  cockroachlabs |     false     | tables      | cockroachlabs | ALL
  cockroachlabs |     false     | types       | cockroachlabs | ALL
  cockroachlabs |     false     | types       | public        | USAGE
(5 rows)
icon/buttons/copy
> CREATE TABLE tracks (
        id UUID PRIMARY KEY,
        album_id UUID,
        title STRING,
        length DECIMAL
);

In the same database, run the following statements as the max user:

icon/buttons/copy
> DROP TABLE albums;
ERROR: user max does not have DROP privilege on relation albums
SQLSTATE: 42501
icon/buttons/copy
> SHOW COLUMNS FROM albums;
  column_name | data_type | is_nullable | column_default | generation_expression |  indices  | is_hidden
--------------+-----------+-------------+----------------+-----------------------+-----------+------------
  id          | UUID      |    false    | NULL           |                       | {primary} |   false
  title       | STRING    |    true     | NULL           |                       | {primary} |   false
  length      | DECIMAL   |    true     | NULL           |                       | {primary} |   false
  tracklist   | JSONB     |    true     | NULL           |                       | {primary} |   false
(4 rows)

max still has SELECT privileges on albums because when cockroachlabs created albums, max was granted default SELECT privileges on all tables created by cockroachlabs.

icon/buttons/copy
> REVOKE SELECT ON TABLE albums FROM max;
icon/buttons/copy
> DROP TABLE tracks;
ERROR: user max does not have DROP privilege on relation tracks
SQLSTATE: 42501
icon/buttons/copy
> SHOW COLUMNS FROM tracks;
ERROR: user max has no privileges on relation tracks
SQLSTATE: 42501

cockroachlabs created the tracks table after revoking default SELECT privileges from max. As a result, max never had SELECT privileges on tracks.

Because max has no default privileges, the user can now be dropped:

icon/buttons/copy
> DROP USER max;
icon/buttons/copy
> SHOW USERS;
    username    | options | member_of
----------------+---------+------------
  admin         |         | {}
  cockroachlabs |         | {}
  root          |         | {admin}
(3 rows)

Grant default privileges for all roles

Run the following statements as a member of the admin role, with ALL privileges:

icon/buttons/copy
> ALTER DEFAULT PRIVILEGES FOR ALL ROLES GRANT SELECT ON TABLES TO public;
icon/buttons/copy
> SHOW DEFAULT PRIVILEGES FOR ALL ROLES;
  role | for_all_roles | object_type | grantee | privilege_type
-------+---------------+-------------+---------+-----------------
  NULL |     true      | tables      | public  | SELECT
  NULL |     true      | types       | public  | USAGE
(2 rows)

In the same database, run the following statements as any two different users:

icon/buttons/copy
> CREATE TABLE discographies (
        id UUID PRIMARY KEY,
        artist STRING,
        total_length DECIMAL
);
Note:

CREATE TABLE requires the CREATE privilege on the database.

icon/buttons/copy
> SHOW COLUMNS FROM discographies;
  column_name  | data_type | is_nullable | column_default | generation_expression |  indices  | is_hidden
---------------+-----------+-------------+----------------+-----------------------+-----------+------------
  id           | UUID      |    false    | NULL           |                       | {primary} |   false
  artist       | STRING    |    true     | NULL           |                       | {primary} |   false
  total_length | DECIMAL   |    true     | NULL           |                       | {primary} |   false
(3 rows)

See also


Yes No
On this page

Yes No