Skip to main content
Use the ALTER ROLE statement to add, change, or remove a role’s password, change the role options for a role, and set default session variable values for a role. You can use the keywords ROLE and USER interchangeably. ALTER USER is an alias for ALTER ROLE.

Considerations

Password management

Password creation and alteration is supported only in secure clusters.

Session variable precedence

When a session starts, CockroachDB determines the initial value of each session variable by evaluating the settings in the following order (items earlier in the list take precedence over later items):
  1. Connection string parameters: A value supplied as a query parameter in the connection URL (for example, .../movr?sslmode=disable&timezone=UTC ).
  2. Per-role, per-database defaults: A value set by ALTER ROLE {role_name} IN DATABASE {db_name} SET {var}={value}.
  3. Per-role, all-database defaults: A value set by ALTER ROLE {role_name} SET {var}={value}.
  4. All-role, per-database defaults: A value set by ALTER ROLE ALL IN DATABASE {db_name} SET {var}={value} or equivalently by ALTER DATABASE {db_name} SET {var}={value}.
  5. Cluster-wide defaults for all roles and all databases: A value set by ALTER ROLE ALL SET {var}={value}.
If a session variable is not modified using any of the preceding methods, the built-in system default value is used. Note that the root user is exempt from settings 3–5. The root user is only affected by values specified in the connection string. You can also set session variables for the duration of a single transaction by using SET LOCAL {var}={value}.
Changes to defaults using the preceding methods only apply to future sessions. This is because session variable resolution happens at session start time. To change a default value in an existing open session, set the variable explicitly with SET.

Required privileges

  • To alter an admin role, the user must be a member of the admin role.
  • To alter other roles, the user must be a member of the admin role or have the CREATEROLE role option.
  • New in v25.4: For per-database defaults, the owner of a database can execute ALTER ROLE ALL IN DATABASE... {SET|RESET} for that database (in addition to users who meet the general requirements above).

Synopsis

alter_role syntax diagram

Parameters

Role options

Examples

The following statements are run by the root user that is a member of the admin role and has ALL privileges.

Allow a role to log in to the database using a password

The following example allows a role to log in to the database with a password:

Prevent a role from using password authentication

The following statement prevents the user from using password authentication and mandates certificate-based client authentication:

Allow a role to create other roles and manage authentication methods for the new roles

The following example allows the role to create other roles and manage authentication methods for them:

Allow a role to create and rename databases

The following example allows the role to create or rename databases:

Allow a role to pause, resume, and cancel non-admin jobs

The following example allows the role to pause, resume, and cancel jobs:

Allow a role to see and cancel non-admin queries and sessions

The following example allows the role to cancel queries and sessions for other non-admin roles:

Allow a role to control changefeeds

The following example allows the role to run CREATE CHANGEFEED:

Allow a role to modify cluster settings

The following example allows the role to modify cluster settings:

Allow a role to bypass row-level security (RLS)

To allow a role to bypass row-level security, execute the following statement to grant the BYPASSRLS privilege:
To disable the role’s ability to bypass RLS, execute the following statement:

Set default session variable values for a role

In the following example, the root user creates a role named max, and sets the default value of the timezone session variable for the max role.
This statement does not affect the default timezone value for any role other than max:
To see the default timezone value for the max role, run the SHOW statement as a member of the max role:
For a list of the session variables that have been updated from default values, see SHOW DEFAULT SESSION VARIABLES FOR ROLE.

Set default session variable values for a role in a specific database

In the following example, the root user creates a role named max and a database named movr, and sets the default value of the statement_timeout session variable for the max role in the movr database.
This statement does not affect the default statement_timeout value for any role other than max, or in any database other than movr.
To see the new default statement_timeout value for the max role, run the SHOW statement as a member of the max role that has connected to the cluster, with the database movr specified in the connection string.
For a list of the session variables that have been updated from default values, see SHOW DEFAULT SESSION VARIABLES FOR ROLE.

Set default session variable values for a specific database

In the following example, the root user creates a database named movr, and sets the default value of the timezone session variable for all roles in that database. New in v25.4: The owner of a database can also execute ALTER ROLE ALL IN DATABASE... {SET|RESET} for that database.
This statement does not affect the default timezone value for any database other than movr:
To see the default timezone value for the max role, run the SHOW statement as a member of the max role:
For a list of the session variables that have been updated from default values, see SHOW DEFAULT SESSION VARIABLES FOR ROLE.

Set default session variable values for all users

To set a default value for all users for any session variable that applies during login, issue a statement like the following:
The root user is exempt from session variable settings changes. This is a failsafe; if you make a mistake with a session variable setting, you can still log in as root and be unaffected.
Use ALTER ROLE ALL SET {sessionvar} = {val} instead of the sql.defaults.* cluster settings. This allows you to set a default value for all users for any session variable that applies during login, making the sql.defaults.* cluster settings redundant.
For a list of the session variables that have been updated from default values, see SHOW DEFAULT SESSION VARIABLES FOR ROLE.

Set the SUBJECT role option for certificate based authentication

You can associate an X.509 certificate’s Subject with a role as shown below. Note that the Subject fields in the certificate have to be an exact match with what you pass in via the SQL statement. By exact match, we mean that the order of attributes passed in via the SQL statement must match the order of attributes in the certificate.
If you manage your own Certificate Authority (CA) infrastructure, CockroachDB supports mapping between the Subject field of your X.509 certificates and SQL roles. For more information, see Certificate-based authentication using multiple values from the X.509 Subject field.

See also