ALTER DATABASE applies a to a database.
This schema change statement is registered as a job. You can view long-running jobs with .
Required privileges
Refer to the respective subcommands.Synopsis
Parameters
| Parameter | Description |
|---|---|
database\_name | The name of the database you want to change. |
Subcommands
| Subcommand | Description |
|---|---|
ADD REGION | Add a region to a . |
ADD SUPER REGION | Add a super region made up of a set of such that data from will be stored in only those regions. |
ALTER LOCALITY | Persistently modify the configuration generated by the standard on a per-region basis. For advanced users who want to customize their multi-region setup using , a persistent, composable alternative to low-level . |
ALTER SUPER REGION | Alter an existing to include a different set of regions. A super region is made up of a set of regions added with ADD REGION such that data from will be stored in only those regions. |
CONFIGURE ZONE | for a database. |
DROP REGION | Drop a region from a . |
DROP SECONDARY REGION | Drop a from a . |
DROP SUPER REGION | Drop a super region made up of a set of . |
OWNER TO | Change the owner of a database. |
PLACEMENT | Configure the replica placement policy for a . |
RENAME TO | Change the name of a database. |
RESET {session variable} | Reset the session variable values for the database to the system defaults. This syntax is identical to . |
SET {session variable} | Set the default session variable values for the database. This syntax is identical to . |
SET PRIMARY REGION | Set the primary region of a . |
SET SECONDARY REGION | Set the for failover purposes. |
SURVIVE {ZONE,REGION} FAILURE | Add a survival goal to a . |
ADD REGION
ALTER DATABASE.. ADD REGION adds a to a . While CockroachDB processes an index modification or changing a table to or from a , attempting to drop a region from the database containing that REGIONAL BY ROW table will produce an error. Similarly, while this statement is running, all index modifications and locality changes on tables will be blocked.
In order to add a region with
ADD REGION, you must first set a primary database region with SET PRIMARY REGION, or at . For an example showing how to add a primary region with ALTER DATABASE, see Set the primary region.Required privileges
To add a region to a database, the user must have one of the following:- Membership to the role for the cluster.
- Either or the for the database and all tables in the database.
Parameters
| Parameter | Description |
|---|---|
region\_name | The being added to this database. Allowed values include any region present in . |
ADD SUPER REGION
ALTER DATABASE.. ADD SUPER REGION adds a to a .
This feature is in and subject to change. To share feedback and/or issues, contact Support.
- Your cluster must be a .
- Super regions .
- Super regions can only contain one or more that have already been added with .
- Each database region can only belong to one super region. In other words, given two super regions A and B, the set of database regions in A must be disjoint from the set of database regions in B.
- You cannot that is part of a super region until you either to remove it, or altogether.
Required privileges
To add a super region to a database, the user must have one of the following:- Membership to the role for the cluster.
- Either or the for the database.
Parameters
| Parameter | Description |
|---|---|
region\_name | The name of the being added to this database. |
region\_name\_list | The super region consists of this set of . |
ALTER LOCALITY
ALTER DATABASE.. ALTER LOCALITY provides a customization tool for advanced users to persistently modify the configuration generated by the standard on a per-region basis.
The feature this statement enables is also known as Zone Config Extensions. For more information about how it works, and the benefits over using the low-level CONFIGURE ZONE statement, see .
For examples, see Use Zone Config Extensions.
Required privileges
The user must be a member of the or roles, or have the on the database.Parameters
| Parameter | Description |
|---|---|
GLOBAL | Apply the Zone Configuration Extension to . |
REGIONAL | Apply the Zone Configuration Extension to all and tables. |
REGIONAL IN | Apply the Zone Configuration Extension to all REGIONAL BY TABLE IN {region} tables and all of REGIONAL BY ROW tables). |
variable | The name of the to change for the in the specified . |
value | The value of the to change for the in the specified . |
ALTER SUPER REGION
ALTER DATABASE.. ALTER SUPER REGION alters an existing of a .
This feature is in and subject to change. To share feedback and/or issues, contact Support.
- Your cluster must be a .
- Super regions .
- Super regions can only contain one or more that have already been added with .
- Each database region can only belong to one super region. In other words, given two super regions A and B, the set of database regions in A must be disjoint from the set of database regions in B.
- You cannot that is part of a super region until you either to remove it, or altogether.
Required privileges
To alter a database’s super region, the user must have one of the following:- Membership to the role for the cluster.
- Either or the for the database.
Parameters
| Parameter | Description |
|---|---|
region\_name | The name of the being altered. |
region\_name\_list | The altered super region will consist of this set of . |
CONFIGURE ZONE
ALTER DATABASE... CONFIGURE ZONE is used to add, modify, reset, or remove replication zones for a database. To view details about existing replication zones, use . For more information about replication zones, see .
If you directly change a database’s zone configuration with
ALTER DATABASE... CONFIGURE ZONE, CockroachDB will block all ALTER DATABASE... SET PRIMARY REGION statements on the database.Cockroach Labs .Most users should use instead. If additional control is needed, use to augment the multi-region SQL statements.
Required privileges
The user must be a member of the or have been granted or privileges. To configure , the user must be a member of theadmin role.
Parameters
| Parameter | Description |
|---|---|
variable | The name of the to change. |
value | The value of the to change. |
DISCARD | Remove a replication zone. |
DROP REGION
ALTER DATABASE.. DROP REGION drops a from a . While CockroachDB processes an index modification or changing a table to or from a , attempting to drop a region from the database containing that REGIONAL BY ROW table will produce an error. Similarly, while this statement is running, all index modifications and locality changes on tables will be blocked.
You cannot drop a region from a multi-region database if:
- The region is set as the primary region, unless it is the last remaining region.
- The region is set as the secondary region. To drop the region, you must first unset the secondary region using
DROP SECONDARY REGION. - The database uses the and there are only three regions configured on the database.
Required privileges
To drop a region from a database, the user must have one of the following:- Membership to the role for the cluster.
- Membership to the role, or the , for the database and all tables in the database.
Parameters
| Parameter | Description |
|---|---|
region\_name | The being dropped from this database. Allowed values include any region present in . You can only drop the primary region from a multi-region database if it is the last remaining region. |
DROP SECONDARY REGION
ALTER DATABASE.. DROP SECONDARY REGION unsets the (if set) from a .
The secondary region is used for failover purposes: if the primary region fails, the secondary region acts as the new primary region. For more information, see .
For examples, see Manage regions.
Required privileges
To unset a secondary region on a database, the user must have one of the following:- Membership to the role for the cluster.
- Membership to the role, or the for the database.
DROP SUPER REGION
ALTER DATABASE.. DROP SUPER REGION drops a from a .
This feature is in and subject to change. To share feedback and/or issues, contact Support.
- Your cluster must be a .
- Super regions .
- Super regions can only contain one or more that have already been added with .
- Each database region can only belong to one super region. In other words, given two super regions A and B, the set of database regions in A must be disjoint from the set of database regions in B.
- You cannot that is part of a super region until you either to remove it, or altogether.
Required privileges
To drop a super region from a database, the user must have one of the following:- Membership to the role for the cluster.
- Either or the for the database.
Parameters
| Parameter | Description |
|---|---|
region\_name | The name of the being dropped from this database. |
OWNER TO
ALTER DATABASE... OWNER TO is used to change the owner of a database.
For examples, see Change database owner.
Required privileges
To change the owner of a database, the user must be anadmin user, or the current owner of the database and a member of the new owner . The user must also have the CREATEDB .
Parameters
| Parameter | Description |
|---|---|
role\_spec | The role to set as the owner of the database. |
PLACEMENT
ALTER DATABASE... PLACEMENT is used to set the replica placement policy (DEFAULT or RESTRICTED) for a ’s . are those with or localities.
ALTER DATABASE... PLACEMENT RESTRICTED constrains the voting and non-voting replica placement for the database’s tables to the associated with those tables. This is a way of opting out of for to accomplish one or more of the following goals:
- Implement a strategy.
- Reduce the amount of data stored on the cluster.
- Reduce the overhead of replicating data across a large number of regions (e.g., 10 or more) for databases with heavier write loads.
ALTER DATABASE... PLACEMENT RESTRICTED does not allow you to opt out of placing entirely. For example, in the database will remain unaffected by this statement. GLOBAL tables are designed to have replicas placed across all available to ensure fast local reads.
For examples, see Set replica placement policy.
Required privileges
To use this statement, the user must have one of the following:- Membership to the role for the cluster.
- or the for the database and all tables in the database.
Parameters
| Parameter | Description |
|---|---|
DEFAULT | Set the replica placement policy for to DEFAULT. |
RESTRICTED | Set the replica placement policy for to RESTRICTED. |
DEFAULT (Default): If the replica placement policy is set to DEFAULT, CockroachDB will use its default replica placement settings, which mean that:
- Data will be placed in as many regions as necessary to ensure your are met.
- You can get fast stale reads from all .
-
RESTRICTED: If the replica placement policy is set toRESTRICTED, CockroachDB will constrain replica placement to only those regions where the table has voting replicas (that is, replicas which participate in the ). In practice, this means that voting replicas for the table will be constrained to the table’s . Specifically, for tables, it will only place replicas in the defined region (or the database’s primary region); for tables, it will only place replicas for each underlying in the partition’s specified region. Finally, note that: - Regional tables with this placement setting will no longer provide “fast stale reads” from other (non-home) regions, since fast stale reads rely on the presence of non-voting replicas.
- The
RESTRICTEDreplica placement policy is only available for databases with the . - This setting does not affect how work; they will still place replicas in all .
RENAME TO
ALTER DATABASE... RENAME TO changes the name of a database.
It is not possible to rename a database if the database is referenced by a . For more details, see .
Required privileges
To rename a database, the user must be a member of theadmin role or must have the parameter set.
Parameters
| Parameter | Description |
|---|---|
database\_new\_name | The you want to use for the database. The new name must be unique and follow these . You cannot rename a database if it is set as the or if . |
RESET session variable
ALTER DATABASE... RESET session variable clears a database-level override of a so that future sessions use the default value.
For a list of the session variables that have been updated from default values, see .
Required privileges
No are required to reset a session setting.Parameters
| Parameter | Description |
|---|---|
session\_var | The name of the . |
Aliases
In CockroachDB, the following are aliases forALTER DATABASE... RESET session variable:
SET session variable
ALTER DATABASE... SET session variable sets the default value of a for all future sessions on the database. This command does not alter the session setting of the current session.
For a list of the session variables that have been updated from default values, see .
Required privileges
To set therole session variable, the current user must be a member of the admin role, or a member of the target role.
All other session variables do not require to modify.
Parameters
| Parameter | Description |
|---|---|
variable | The name of to set. The variable name is case-insensitive. |
value | The value, or list of values, to assign to the session variable. |
Aliases
In CockroachDB, the following are aliases forALTER DATABASE... SET session variable:
ALTER ROLE ALL IN DATABASE... SET session variable
Session variable precedence
When a starts, CockroachDB determines the initial value of each by evaluating the settings in the following order (items earlier in the list take precedence over later items):- : A value supplied as a query parameter in the connection URL (for example,
.../movr?sslmode=disable&timezone=UTC). - : A value set by
ALTER ROLE {role_name} IN DATABASE {db_name} SET {var}={value}. - : A value set by
ALTER ROLE {role_name} SET {var}={value}. - : A value set by
ALTER ROLE ALL IN DATABASE {db_name} SET {var}={value}or equivalently byALTER DATABASE {db_name} SET {var}={value}. - : A value set by
ALTER ROLE ALL SET {var}={value}.
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 .
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 PRIMARY REGION
ALTER DATABASE.. SET PRIMARY REGION sets the primary of a .
If a database’s has been directly set with an
ALTER DATABASE... CONFIGURE ZONE statement, CockroachDB will block all ALTER DATABASE... SET PRIMARY REGION statements on the database.To remove existing, manually-configured zones from a database (and unblock SET PRIMARY REGION statements on the database), use an ALTER DATABASE... CONFIGURE ZONE DISCARD statement.Required privileges
To add a primary region to a database with no existing regions, the user must have one of the following:- Membership to the role for the cluster.
- Membership to the role, or the , for the database and all tables in the database.
Parameters
| Parameter | Description |
|---|---|
region\_name | The region to set as the database’s primary region. Allowed values include any region present in . |
SET SECONDARY REGION
ALTER DATABASE.. SET SECONDARY REGION adds a to a for failover purposes.
If the primary region fails, the secondary region becomes the new primary region. For more information, see .
In order to add a secondary region with ALTER DATABASE... SET SECONDARY REGION, you must first set a primary database region with SET PRIMARY REGION, or when . For an example showing how to add a secondary region with ALTER DATABASE, see Set the secondary region.
are not compatible with databases containing tables. CockroachDB does not prevent you from defining secondary regions on databases with regional by row tables, but the interaction of these features is not supported.
Therefore, Cockroach Labs recommends that you avoid defining secondary regions on databases that use regional by row table configurations.
For examples, see Manage regions.
Required privileges
To add a secondary region to a database, the user must have one of the following:- Membership to the role for the cluster.
- Either or the for the database.
Parameters
| Parameter | Description |
|---|---|
region\_name | Usually, the being set as the secondary region for this database. E.g., "ap-southeast-2". Allowed values include any (non-primary) region present in SHOW REGIONS. |
SURVIVE {ZONE,REGION} FAILURE
ALTER DATABASE... SURVIVE {ZONE,REGION} FAILURE sets the for a .
For examples, see Configure survival goals.
Required privileges
The user must be a member of the or roles, or have the on the database.Parameters
| Parameter | Description |
|---|---|
ZONE | Set the to zone failure. |
REGION | Set the to region failure. |
Examples
Manage regions
Setup
Only a specified can be used as a . To follow along with the examples in this section, start a with the to simulate a multi-region cluster:Set the primary region
Suppose you want to make the databasemovr a multi-region database.
To add the first region to the database, or to set an already-added region as the primary region, use a SET PRIMARY REGION statement:
- All tables will be in the primary region by default.
- This means that all such tables will have all of their voting replicas and leaseholders moved to the primary region. This process is known as .
Add regions to a database
To add more regions to a database that already has at least one region:Set the secondary region
To set an existing (that is not already the primary region) as the , use the following statement:"us-west1" region will act as the primary region if the original primary region fails.
are not compatible with databases containing tables. CockroachDB does not prevent you from defining secondary regions on databases with regional by row tables, but the interaction of these features is not supported.
Therefore, Cockroach Labs recommends that you avoid defining secondary regions on databases that use regional by row table configurations.
Unset the secondary region
To unset an existing as the on a multi-region database, use theDROP SECONDARY REGION statement:
This statement does not drop the region from the database. To drop a region, use
DROP REGION.Change the existing primary region
To change the primary region to another region in the database, use aSET PRIMARY REGION statement.
Drop a region from a database
To drop a region from a multi-region database, use aDROP REGION statement.
- The region is set as the primary region, unless it is the last remaining region.
- The region is set as the secondary region. To drop the region, you must first unset the secondary region using
DROP SECONDARY REGION. - The database uses the and there are only three regions configured on the database.
Manage super regions
Setup
Only a specified can be used as a . To follow along with the examples in this section, start a with the to simulate a multi-region cluster:Set up MovR database regions
Execute the following statements. They will tell CockroachDB about the database’s regions. This information is necessary so that CockroachDB can later move data around to optimize access to particular data from particular regions. For more information about how this works at a high level, see .Set up MovR global tables
Because the data inpromo_codes is not updated frequently (a.k.a., “read-mostly”), and needs to be available from any region, the right table locality is .
user_promo_codes table to have a foreign key into the global promo_codes table. This will enable fast reads of the promo_codes.code column from any region in the cluster.
Set up MovR regional tables
All of the tables exceptpromo_codes contain rows which are partitioned by region, and updated very frequently. For these tables, the right table locality for optimizing access to their data is .
Apply this table locality to the remaining tables. These statements use a CASE statement to put data for a given city in the right region and can take around 1 minute to complete for each table.
-
rides -
user_promo_codes -
users -
vehicle_location_histories -
vehicles
Enable super regions
To enable super regions, set theenable_super_regions to 'on':
sql.defaults.super_regions.enabled to true:
Add a super region to a database
To add a super region to a multi-region database, use theALTER DATABASE... ADD SUPER REGION statement:
Alter a super region
This example assumes you have already added a"usa" super region as shown in the example Add a super region to a database. If you wanted to drop the region us-west1, you would first need to remove it from the super region.
To remove a region from a super region, use the ALTER DATABASE... ALTER SUPER REGION statement and list only the regions that should remain in the super region:
Allow user to modify a primary region that is part of a super region
By default, you may not change the of a when that region is part of a super region. This is a safety setting designed to prevent you from accidentally moving the data for a that is meant to be stored in the super region out of that super region, which could break your data domiciling setup. If you are sure about what you are doing, you can allow modifying the primary region by setting thealter_primary_region_super_region_override to 'on':
sql.defaults.alter_primary_region_super_region_override.enable to true:
Drop a super region from a database
To drop a super region from a multi-region database, use aDROP SUPER REGION statement:
ADD SUPER REGION:
Configure replication zones
Cockroach Labs .Most users should use instead. If additional control is needed, use to augment the multi-region SQL statements.
Setup
The following examples use MovR, a fictional vehicle-sharing application, to demonstrate CockroachDB SQL statements. For more information about the MovR example application and dataset, see . To follow along, run with the--geo-partitioned-replicas flag. This command opens an interactive SQL shell to a temporary, 9-node in-memory cluster with the movr database.
Create a replication zone for a database
To control replication for a specific database, use theALTER DATABASE... CONFIGURE ZONE statement to define the relevant values (other values will be inherited from the parent zone):
Edit a replication zone
Remove a replication zone
When you discard a zone configuration, the objects it was applied to will then inherit a configuration from an object “the next level up”; e.g., if the object whose configuration is being discarded is a table, it will use its parent database’s configuration.You cannot
DISCARD any zone configurations on multi-region tables, indexes, or partitions if the created the zone configuration.Troubleshoot replication zones
For instructions showing how to troubleshoot replication zones that may be misconfigured, see .Use Zone Config Extensions
The following examples show:- How to override specific fields of a schema object’s zone configs.
- How something like the multi-region abstraction could have been implemented using Zone Config Extensions. For this example, we will call it “Failover regions”.
- How to reset a region’s Zone Config Extensions.
- How to discard a region’s Zone Config Extensions.
- How you could implement super regions.
- How to minimize cross-region write latency.
We strongly recommend using the multi-region abstractions over “rolling your own” using Zone Config Extensions. These examples are provided to show the flexibility of Zone Config Extensions.
Setup
The setup described in this section will be used in the following examples.Start a cluster
Start a cluster as follows:Make the database multi-region
Next, modify the database to use the as follows:- Set the primary region using
SET PRIMARY REGION. - Add the other two regions using
ADD REGION.
Override specific fields of a schema object’s zone configs
In this example we will configure a multi-region database to update its field. Setting the primary region tous-east1 during the setup steps added us-east1 to lease_preferences.
ALTER DATABASE... ALTER LOCALITY to overwrite the lease_preferences field to add us-west1 to the list of regions:
lease_preferences field is now updated to include us-west1.
To remove the changes made in this example, reset the Zone Config Extensions.
Implement super regions
In this example, are used to provide an alternative implementation of , which are useful for . For this example, you need to start a using the following command, which :- Make the into a by .
- Set the
movrdatabase to . - Add the remaining regions defined at cluster start time to the multi-region
movrdatabase (for a total of 6).
ALTER LOCALITY statements to such that:
- in the
us-east1region are only stored on nodes in the US. - Replicas in the
europe-central1region are only stored on nodes in the EU.
Minimize cross-region write latency
In this example, are used to minimize cross-region write latency in a cluster with the following characteristics:- Three main US regions (
us-west1,us-east1,us-central1). - Two additional distant regions (
europe-west1,asia-northeast1). Only will be made from these regions.
- Make the into a by .
- Set the
movrdatabase to . - Add the remaining regions defined at cluster start time to the multi-region
movrdatabase (for a total of 5).
movr database are :
ALTER LOCALITY statements to such that:
- The 3 US regions have voting replicas (that is, replicas that participate in ).
- The 2 additional regions (Europe and Japan) are left with only that can be used to serve .
Failover regions
In this example we will use Zone Config Extensions to configure a multi-region database so that if the primary region fails, enough will be found in another region (the “failover” region) to take over for the primary region. We will set the locality scope for this configuration to beREGIONAL IN, which covers all and all of .
This functionality is already provided by the built-in feature. It is used here to show the flexibility of Zone Config Extensions. We strongly recommend using the whenever possible.
-
Set the database to have a
REGIONsurvival goal using : -
Apply the
REGIONAL BY ROWlocality to themovr.ridestable using the following statement: -
View the for the
movr.ridestable using :Remember that we configuredus-east1to be our primary region during cluster setup. The output above confirms thatus-east1is the primary region based on the values of the and keys. -
Update the configuration to keep additional voting replicas and leaseholders in
us-west1. We do this because we would like to configureus-west1to be the failover region forus-east1. The following SQL statement accomplishes this by configuringus-east1to keep additional voting replicas and leaseholders inus-west1. This means that ifus-east1fails, it will fail over tous-west1. -
View the for the
movr.ridestable using :The following changes are shown: - There are now 2 voting replicas stored inus-west1. - There is now a preference that if leases cannot be placed inus-east1, they should be placed inus-west1. Both of these changes combine to ensure that ifus-east1goes down, the cluster will still be able to operate until some mitigation is in place.
Reset a region’s Zone Config Extensions
To reset the Zone Config Extension configuration applied to a region to the default settings, enter the following statement:This will not reset any configuration created by the .
Discard a region’s Zone Config Extensions
To discard the Zone Config Extension settings from a region, enter the following statement:Troubleshoot Zone Config Extensions
The process for troubleshooting Zone Config Extensions is the same as troubleshooting any other changes to zone configs. For instructions showing how to troubleshoot replication zones that may be misconfigured, see .Change database owner
Setup
To follow along, run to start a temporary, in-memory cluster with the sample dataset preloaded:Change a database’s owner
Suppose that the current owner of themovr database is root and you want to change the owner to a new user named max.
max, query the pg_catalog.pg_database and pg_catalog.pg_roles tables:
If the user running the command is not an admin user, they must own the database and be a member of the new owning role. They must also have the
CREATEDB .Set replica placement policy
Setup
To follow along with the examples below:-
Start a with the to simulate a multi-region cluster:
- Set the demo cluster’s and as described in (specifically, starting at ).
-
Enable the replica placement syntax with either the or the as shown below.
-
To use the session variable:
-
To use the cluster setting:
-
To use the session variable:
Use instead of the
sql.defaults.* . This allows you to set a default value for all users for any that applies during login, making the sql.defaults.* cluster settings redundant.Create a database with the replica placement policy set to restricted
If you know at database creation time that you’d like to set the database’s replica placement policy to “restricted”, you can do so in a statement as shown below:Set the replica placement policy to RESTRICTED
When you set the database’s placement policy to RESTRICTED, you are saying that you want the underlying data to be restricted to the table or partition’s .
Set the replica placement policy to DEFAULT
If previously you set the replica placement policy to RESTRICTED, you can set it back to the default by issuing the following statement:
Rename database
Rename a database
Configure survival goals
Survive zone failures
To change the survival goal of a multi-region database to survive zone failures, use the following statement:Surviving zone failures is the default setting for multi-region databases.

