> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ALTER DATABASE

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

The `ALTER DATABASE` <InternalLink path="sql-statements">statement</InternalLink> applies a <InternalLink path="online-schema-changes">schema change</InternalLink> to a database.

<Note>
  This schema change statement is registered as a job. You can view long-running jobs with <InternalLink path="show-jobs">`SHOW JOBS`</InternalLink>.
</Note>

## Required privileges

Refer to the respective [subcommands](#subcommands).

## Synopsis

<img src="https://mintcdn.com/cockroachlabs/ulDoMGhKYg9RNabd/images/sql-diagrams/v23.1/alter_database.svg?fit=max&auto=format&n=ulDoMGhKYg9RNabd&q=85&s=3f4a7e7de89715b1c44e3cc23f5fa117" alt="alter_database syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="1987" height="1463" data-path="images/sql-diagrams/v23.1/alter_database.svg" />

## Parameters

| Parameter       | Description                                  |
| --------------- | -------------------------------------------- |
| `database_name` | The name of the database you want to change. |

Additional parameters are documented for the respective [subcommands](#subcommands).

## Subcommands

| Subcommand                                                      | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [`ADD REGION`](#add-region)                                     | Add a region to a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.                                                                                                                                                                                                                                                                                                                                                                        |
| [`ADD SUPER REGION`](#add-super-region)                         | Add a super region made up of a set of <InternalLink path="multiregion-overview#super-regions">database regions</InternalLink> such that data from <InternalLink path="regional-tables">regional tables</InternalLink> will be stored in only those regions.                                                                                                                                                                                                             |
| [`ALTER LOCALITY`](#alter-locality)                             | Persistently modify the configuration generated by the standard <InternalLink path="multiregion-overview">multi-region SQL abstractions</InternalLink> on a per-region basis. For advanced users who want to customize their multi-region setup using <InternalLink path="zone-config-extensions">Zone Config Extensions</InternalLink>, a persistent, composable alternative to low-level <InternalLink path="configure-replication-zones">zone configs</InternalLink>. |
| [`ALTER SUPER REGION`](#alter-super-region)                     | Alter an existing <InternalLink path="multiregion-overview#super-regions">super region</InternalLink> to include a different set of regions. A super region is made up of a set of regions added with [`ADD REGION`](#add-region) such that data from <InternalLink path="regional-tables">regional tables</InternalLink> will be stored in only those regions.                                                                                                          |
| [`CONFIGURE ZONE`](#configure-zone)                             | <InternalLink path="configure-replication-zones">Replication Controls</InternalLink> for a database.                                                                                                                                                                                                                                                                                                                                                                     |
| [`DROP REGION`](#drop-region)                                   | Drop a region from a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.                                                                                                                                                                                                                                                                                                                                                                     |
| [`DROP SECONDARY REGION`](#drop-secondary-region)               | Drop a <InternalLink path="multiregion-overview#secondary-regions">secondary region</InternalLink> from a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.                                                                                                                                                                                                                                                                                |
| [`DROP SUPER REGION`](#drop-super-region)                       | Drop a super region made up of a set of <InternalLink path="multiregion-overview#super-regions">database regions</InternalLink>.                                                                                                                                                                                                                                                                                                                                         |
| [`OWNER TO`](#owner-to)                                         | Change the owner of a database.                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| [`PLACEMENT`](#placement)                                       | Configure the replica placement policy for a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.                                                                                                                                                                                                                                                                                                                                             |
| [`RENAME TO`](#rename-to)                                       | Change the name of a database.                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| [`RESET {session variable}`](#reset-session-variable)           | Reset the session variable values for the database to the system defaults. This syntax is identical to <InternalLink path="alter-role#set-default-session-variable-values-for-all-users">`ALTER ROLE ALL IN DATABASE ... RESET {session variable}`</InternalLink>.                                                                                                                                                                                                       |
| [`SET {session variable}`](#set-session-variable)               | Set the default session variable values for the database. This syntax is identical to <InternalLink path="alter-role#set-default-session-variable-values-for-all-users">`ALTER ROLE ALL IN DATABASE ... SET {session variable}`</InternalLink>.                                                                                                                                                                                                                          |
| [`SET PRIMARY REGION`](#set-primary-region)                     | Set the primary region of a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.                                                                                                                                                                                                                                                                                                                                                              |
| [`SET SECONDARY REGION`](#set-secondary-region)                 | Set the <InternalLink path="multiregion-overview#secondary-regions">secondary region of a multi-region database</InternalLink> for failover purposes.                                                                                                                                                                                                                                                                                                                    |
| [`SURVIVE {ZONE,REGION} FAILURE`](#survive-zone-region-failure) | Add a survival goal to a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.                                                                                                                                                                                                                                                                                                                                                                 |

### `ADD REGION`

`ALTER DATABASE .. ADD REGION` adds a <InternalLink path="multiregion-overview#database-regions">region</InternalLink> to a <InternalLink path="multiregion-overview">multi-region database</InternalLink>. While CockroachDB processes an index modification or changing a table to or from a <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW` table</InternalLink>, 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 <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink> tables will be blocked.

<Danger>
  In order to add a region with `ADD REGION`, you must first set a primary database region with [`SET PRIMARY REGION`](#set-primary-region), or at <InternalLink path="create-database">database creation</InternalLink>. For an example showing how to add a primary region with `ALTER DATABASE`, see [Set the primary region](#set-the-primary-region).
</Danger>

For examples, see [Manage regions](#manage-regions).

#### Required privileges

To add a region to a database, the user must have one of the following:

* Membership to the <InternalLink path="security-reference/authorization#admin-role">`admin`</InternalLink> role for the cluster.
* Either <InternalLink path="security-reference/authorization#object-ownership">ownership</InternalLink> or the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink> for the database and all <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink> tables in the database.

#### Parameters

| Parameter     | Description                                                                                                                                                                                                                                    |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `region_name` | The <InternalLink path="multiregion-overview#database-regions">region</InternalLink> being added to this database.  Allowed values include any region present in <InternalLink path="show-regions">`SHOW REGIONS FROM CLUSTER`</InternalLink>. |

For usage, see [Synopsis](#synopsis).

### `ADD SUPER REGION`

`ALTER DATABASE .. ADD SUPER REGION` adds a <InternalLink path="multiregion-overview#super-regions">super region</InternalLink> to a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.

<Note>
  **This feature is in <InternalLink path="cockroachdb-feature-availability">preview</InternalLink>** and subject to change. To share feedback and/or issues, contact [Support](https://support.cockroachlabs.com).
</Note>

To use super regions, keep the following considerations in mind:

* Your cluster must be a <InternalLink path="multiregion-overview">multi-region cluster</InternalLink>.
* Super regions [must be enabled](#enable-super-regions).
* Super regions can only contain one or more <InternalLink path="multiregion-overview#database-regions">database regions</InternalLink> that have already been added with <InternalLink path="alter-database#add-region">`ALTER DATABASE ... ADD REGION`</InternalLink>.
* 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](https://wikipedia.org/wiki/Disjoint_sets) from the set of database regions in *B*.
* You cannot <InternalLink path="alter-database#drop-region">drop a region</InternalLink> that is part of a super region until you either <InternalLink path="alter-database#alter-super-region">alter the super region</InternalLink> to remove it, or <InternalLink path="alter-database#drop-super-region">drop the super region</InternalLink> altogether.

For examples, see [Manage super regions](#manage-super-regions).

#### Required privileges

To add a super region to a database, the user must have one of the following:

* Membership to the <InternalLink path="security-reference/authorization#admin-role">`admin`</InternalLink> role for the cluster.
* Either <InternalLink path="security-reference/authorization#object-ownership">ownership</InternalLink> or the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink> for the database.

#### Parameters

| Parameter          | Description                                                                                                                          |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------ |
| `region_name`      | The name of the <InternalLink path="multiregion-overview#super-regions">super region</InternalLink> being added to this database.    |
| `region_name_list` | The super region consists of this set of <InternalLink path="multiregion-overview#database-regions">database regions</InternalLink>. |

For usage, see [Synopsis](#synopsis).

### `ALTER LOCALITY`

`ALTER DATABASE .. ALTER LOCALITY` provides a customization tool for advanced users to persistently modify the configuration generated by the standard <InternalLink path="multiregion-overview">multi-region SQL abstractions</InternalLink> 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`](#configure-zone) statement, see <InternalLink path="zone-config-extensions">Zone Config Extensions</InternalLink>.

For examples, see [Use Zone Config Extensions](#use-zone-config-extensions).

#### Required privileges

The user must be a member of the <InternalLink path="security-reference/authorization#roles">`admin`</InternalLink> or <InternalLink path="security-reference/authorization#object-ownership">owner</InternalLink> roles, or have the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink> on the database.

#### Parameters

| Parameter     | Description                                                                                                                                                                                                                                                                                                                          |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `GLOBAL`      | Apply the Zone Configuration Extension to <InternalLink path="table-localities#global-tables">global tables</InternalLink>.                                                                                                                                                                                                          |
| `REGIONAL`    | Apply the Zone Configuration Extension to all <InternalLink path="table-localities#regional-tables">`REGIONAL BY TABLE`</InternalLink> and <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink> tables.                                                                                     |
| `REGIONAL IN` | Apply the Zone Configuration Extension to all `REGIONAL BY TABLE IN {region}` tables and all <code>{'{region}'}</code> <InternalLink path="partitioning">partitions</InternalLink> of `REGIONAL BY ROW` tables).                                                                                                                     |
| `variable`    | The name of the <InternalLink path="configure-replication-zones#replication-zone-variables">replication zone variable</InternalLink> to change for the <InternalLink path="schema-design-overview">schema objects</InternalLink> in the specified <InternalLink path="multiregion-overview#database-regions">region</InternalLink>.  |
| `value`       | The value of the <InternalLink path="configure-replication-zones#replication-zone-variables">replication zone variable</InternalLink> to change for the <InternalLink path="schema-design-overview">schema objects</InternalLink> in the specified <InternalLink path="multiregion-overview#database-regions">region</InternalLink>. |

For usage, see [Synopsis](#synopsis).

### `ALTER SUPER REGION`

`ALTER DATABASE .. ALTER SUPER REGION` alters an existing <InternalLink path="multiregion-overview#super-regions">super region</InternalLink> of a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.

<Note>
  **This feature is in <InternalLink path="cockroachdb-feature-availability">preview</InternalLink>** and subject to change. To share feedback and/or issues, contact [Support](https://support.cockroachlabs.com).
</Note>

To use super regions, keep the following considerations in mind:

* Your cluster must be a <InternalLink path="multiregion-overview">multi-region cluster</InternalLink>.
* Super regions [must be enabled](#enable-super-regions).
* Super regions can only contain one or more <InternalLink path="multiregion-overview#database-regions">database regions</InternalLink> that have already been added with <InternalLink path="alter-database#add-region">`ALTER DATABASE ... ADD REGION`</InternalLink>.
* 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](https://wikipedia.org/wiki/Disjoint_sets) from the set of database regions in *B*.
* You cannot <InternalLink path="alter-database#drop-region">drop a region</InternalLink> that is part of a super region until you either <InternalLink path="alter-database#alter-super-region">alter the super region</InternalLink> to remove it, or <InternalLink path="alter-database#drop-super-region">drop the super region</InternalLink> altogether.

For examples, see [Manage super regions](#manage-super-regions).

#### Required privileges

To alter a database's super region, the user must have one of the following:

* Membership to the <InternalLink path="security-reference/authorization#admin-role">`admin`</InternalLink> role for the cluster.
* Either <InternalLink path="security-reference/authorization#object-ownership">ownership</InternalLink> or the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink> for the database.

#### Parameters

| Parameter          | Description                                                                                                                                      |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `region_name`      | The name of the <InternalLink path="multiregion-overview#super-regions">super region</InternalLink> being altered.                               |
| `region_name_list` | The altered super region will consist of this set of <InternalLink path="multiregion-overview#database-regions">database regions</InternalLink>. |

For usage, see [Synopsis](#synopsis).

### `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 <InternalLink path="show-zone-configurations">`SHOW ZONE CONFIGURATIONS`</InternalLink>. For more information about replication zones, see <InternalLink path="configure-replication-zones">Replication Controls</InternalLink>.

<Danger>
  If you directly change a database's zone configuration with `ALTER DATABASE ... CONFIGURE ZONE`, CockroachDB will block all [`ALTER DATABASE ... SET PRIMARY REGION`](#set-primary-region) statements on the database.
</Danger>

Cockroach Labs  <InternalLink path="configure-replication-zones#why-manual-zone-config-management-is-not-recommended">does not recommend modifying zone configurations manually</InternalLink>.  [does not recommend modifying zone configurations manually](#why-manual-zone-config-management-is-not-recommended).

Most users should use <InternalLink path="multiregion-overview">multi-region SQL statements</InternalLink> instead. If additional control is needed, use <InternalLink path="zone-config-extensions">Zone Config Extensions</InternalLink> to augment the multi-region SQL statements.

You can use *replication zones* to control the number and location of replicas for specific sets of data, both when replicas are first added and when they are rebalanced to maintain cluster equilibrium.

For examples, see [Replication Controls](#configure-replication-zones).

#### Required privileges

The user must be a member of the <InternalLink path="security-reference/authorization#admin-role">`admin` role</InternalLink> or have been granted <InternalLink path="security-reference/authorization#supported-privileges">`CREATE`</InternalLink> or <InternalLink path="security-reference/authorization#supported-privileges">`ZONECONFIG`</InternalLink> privileges. To configure <InternalLink path="configure-replication-zones#for-system-data">`system` objects</InternalLink>, the user must be a member of the `admin` role.

#### Parameters

| Parameter  | Description                                                                                                                                      |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `variable` | The name of the <InternalLink path="configure-replication-zones#replication-zone-variables">replication zone variable</InternalLink> to change.  |
| `value`    | The value of the <InternalLink path="configure-replication-zones#replication-zone-variables">replication zone variable</InternalLink> to change. |
| `DISCARD`  | Remove a replication zone.                                                                                                                       |

For usage, see [Synopsis](#synopsis).

### `DROP REGION`

`ALTER DATABASE .. DROP REGION` drops a <InternalLink path="multiregion-overview#database-regions">region</InternalLink> from a <InternalLink path="multiregion-overview">multi-region database</InternalLink>. While CockroachDB processes an index modification or changing a table to or from a <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW` table</InternalLink>, 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 <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink> tables will be blocked.

You cannot drop a region from a multi-region database if:

* The region is [set as the primary region](#set-primary-region), unless it is the last remaining region.
* The region is [set as the secondary region](#set-secondary-region). To drop the region, you must first unset the secondary region using [`DROP SECONDARY REGION`](#drop-secondary-region).
* The database uses the <InternalLink path="multiregion-survival-goals#survive-region-failures">`REGION` survival goal</InternalLink> and there are only three regions configured on the database.

For examples, see [Manage regions](#manage-regions).

#### Required privileges

To drop a region from a database, the user must have one of the following:

* Membership to the <InternalLink path="security-reference/authorization#roles">`admin`</InternalLink> role for the cluster.
* Membership to the <InternalLink path="security-reference/authorization#object-ownership">owner</InternalLink> role, or the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink>, for the database and all <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink> tables in the database.

#### Parameters

| Parameter     | Description                                                                                                                                                                                                                                                                                                                                                                 |
| ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `region_name` | The <InternalLink path="multiregion-overview#database-regions">region</InternalLink> being dropped from this database. Allowed values include any region present in <InternalLink path="show-regions">`SHOW REGIONS FROM DATABASE database_name`</InternalLink>.<br />You can only drop the primary region from a multi-region database if it is the last remaining region. |

For usage, see [Synopsis](#synopsis).

### `DROP SECONDARY REGION`

`ALTER DATABASE .. DROP SECONDARY REGION` unsets the <InternalLink path="multiregion-overview#secondary-regions">secondary region</InternalLink> (if set) from a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.

The secondary region is used for failover purposes: if the [primary region](#set-primary-region) fails, the secondary region acts as the new primary region. For more information, see <InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink>.

For examples, see [Manage regions](#manage-regions).

#### Required privileges

To unset a secondary region on a database, the user must have one of the following:

* Membership to the <InternalLink path="security-reference/authorization#roles">`admin`</InternalLink> role for the cluster.
* Membership to the <InternalLink path="security-reference/authorization#object-ownership">owner</InternalLink> role, or the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink> for the database.

### `DROP SUPER REGION`

`ALTER DATABASE .. DROP SUPER REGION` drops a <InternalLink path="multiregion-overview#super-regions">super region</InternalLink> from a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.

<Note>
  **This feature is in <InternalLink path="cockroachdb-feature-availability">preview</InternalLink>** and subject to change. To share feedback and/or issues, contact [Support](https://support.cockroachlabs.com).
</Note>

To use super regions, keep the following considerations in mind:

* Your cluster must be a <InternalLink path="multiregion-overview">multi-region cluster</InternalLink>.
* Super regions [must be enabled](#enable-super-regions).
* Super regions can only contain one or more <InternalLink path="multiregion-overview#database-regions">database regions</InternalLink> that have already been added with <InternalLink path="alter-database#add-region">`ALTER DATABASE ... ADD REGION`</InternalLink>.
* 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](https://wikipedia.org/wiki/Disjoint_sets) from the set of database regions in *B*.
* You cannot <InternalLink path="alter-database#drop-region">drop a region</InternalLink> that is part of a super region until you either <InternalLink path="alter-database#alter-super-region">alter the super region</InternalLink> to remove it, or <InternalLink path="alter-database#drop-super-region">drop the super region</InternalLink> altogether.

For examples, see [Manage super regions](#manage-super-regions).

#### Required privileges

To drop a super region from a database, the user must have one of the following:

* Membership to the <InternalLink path="security-reference/authorization#roles">`admin`</InternalLink> role for the cluster.
* Either <InternalLink path="security-reference/authorization#object-ownership">ownership</InternalLink> or the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink> for the database.

#### Parameters

| Parameter     | Description                                                                                                                           |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `region_name` | The name of the <InternalLink path="multiregion-overview#super-regions">super region</InternalLink> being dropped from this database. |

For usage, see [Synopsis](#synopsis).

### `OWNER TO`

`ALTER DATABASE ... OWNER TO` is used to change the owner of a database.

For examples, see [Change database owner](#change-database-owner).

#### Required privileges

To change the owner of a database, the user must be an `admin` user, or the current owner of the database and a member of the new owner <InternalLink path="security-reference/authorization#roles">role</InternalLink>. The user must also have the `CREATEDB` <InternalLink path="security-reference/authorization#managing-privileges">privilege</InternalLink>.

#### Parameters

| Parameter   | Description                                   |
| ----------- | --------------------------------------------- |
| `role_spec` | The role to set as the owner of the database. |

For usage, see [Synopsis](#synopsis).

### `PLACEMENT`

`ALTER DATABASE ... PLACEMENT` is used to set the replica placement policy (`DEFAULT` or `RESTRICTED`) for a <InternalLink path="multiregion-overview">multi-region database</InternalLink>'s <InternalLink path="regional-tables">regional tables</InternalLink>. <InternalLink path="regional-tables">Regional tables</InternalLink> are those with <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink> or <InternalLink path="table-localities#regional-tables">`REGIONAL BY TABLE`</InternalLink> localities.

`ALTER DATABASE ... PLACEMENT RESTRICTED` constrains the voting and non-voting replica placement for the database's tables to the <InternalLink path="alter-table">home regions</InternalLink> associated with those tables. This is a way of opting out of <InternalLink path="architecture/replication-layer#non-voting-replicas">non-voting replicas</InternalLink> for <InternalLink path="regional-tables">regional tables</InternalLink> to accomplish one or more of the following goals:

* Implement a <InternalLink path="data-domiciling">data domiciling</InternalLink> 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.

Note that `ALTER DATABASE ... PLACEMENT RESTRICTED` does not allow you to opt out of placing <InternalLink path="architecture/replication-layer#non-voting-replicas">non-voting replicas</InternalLink> entirely. For example, <InternalLink path="global-tables">`GLOBAL` tables</InternalLink> in the database will remain unaffected by this statement. `GLOBAL` tables are designed to have replicas placed across all available <InternalLink path="multiregion-overview#cluster-regions">cluster regions</InternalLink> to ensure fast local reads.

For examples, see [Set replica placement policy](#set-replica-placement-policy).

#### Required privileges

To use this statement, the user must have one of the following:

* Membership to the <InternalLink path="security-reference/authorization#roles">`admin`</InternalLink> role for the cluster.
* <InternalLink path="security-reference/authorization#object-ownership">Ownership</InternalLink> or the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink> for the database and all tables in the database.

#### Parameters

| Parameter    | Description                                                                                                               |
| ------------ | ------------------------------------------------------------------------------------------------------------------------- |
| `DEFAULT`    | Set the replica placement policy for <InternalLink path="regional-tables">regional tables</InternalLink> to `DEFAULT`.    |
| `RESTRICTED` | Set the replica placement policy for <InternalLink path="regional-tables">regional tables</InternalLink> to `RESTRICTED`. |

For usage, see [Synopsis](#synopsis).

The replica placement policies work as follows:

<a id="parameters-default" /> - `DEFAULT` (**Default**):  If the replica placement policy [is set to `DEFAULT`](#set-the-replica-placement-policy-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 <InternalLink path="multiregion-overview#survival-goals">database survival goals</InternalLink> are met.
* You can get fast stale reads from all <InternalLink path="multiregion-overview#database-regions">database regions</InternalLink>.

<a id="parameters-restricted" /> - `RESTRICTED`:  If the replica placement policy [is set to `RESTRICTED`](#set-the-replica-placement-policy-to-restricted), CockroachDB will constrain replica placement to only those regions where the table has voting replicas (that is, replicas which participate in the <InternalLink path="architecture/replication-layer#raft">Raft quorum</InternalLink>). In practice, this means that voting replicas for the table will be constrained to the table's <InternalLink path="multiregion-overview#table-localities">home region</InternalLink>. Specifically, for <InternalLink path="table-localities#regional-tables">`REGIONAL BY TABLE`</InternalLink> tables, it will only place replicas in the defined region (or the database's [primary region](#set-primary-region)); for <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink> tables, it will only place replicas for each underlying <InternalLink path="partitioning">partition</InternalLink> 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 `RESTRICTED` replica placement policy is only available for databases with the <InternalLink path="multiregion-survival-goals#survive-zone-failures">`ZONE` survival goal</InternalLink>.
* This setting does not affect how <InternalLink path="global-tables">`GLOBAL` tables</InternalLink> work; they will still place replicas in all <InternalLink path="multiregion-overview#database-regions">database regions</InternalLink>.

### `RENAME TO`

`ALTER DATABASE ... RENAME TO` changes the name of a database.

<Note>
  It is not possible to rename a database if the database is referenced by a <InternalLink path="views">view</InternalLink>. For more details, see <InternalLink path="views#view-dependencies">View Dependencies</InternalLink>.
</Note>

For examples, see [Rename database](#rename-database).

#### Required privileges

To rename a database, the user must be a member of the `admin` role or must have the <InternalLink path="create-role#create-a-role-that-can-create-and-rename-databases">`CREATEDB`</InternalLink> parameter set.

#### Parameters

| Parameter           | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `database_new_name` | The <InternalLink path="sql-grammar">`name`</InternalLink> you want to use for the database. The new name must be unique and follow these <InternalLink path="keywords-and-identifiers#identifiers">identifier rules</InternalLink>. You cannot rename a database if it is set as the <InternalLink path="sql-name-resolution#current-database">current database</InternalLink> or if <InternalLink path="set-vars">`sql_safe_updates = true`</InternalLink>. |

For usage, see [Synopsis](#synopsis).

### `RESET {session variable}`

`ALTER DATABASE ... RESET {session variable}` resets a <InternalLink path="set-vars#supported-variables">session variable</InternalLink> for a database to its default value for the client session.

#### Required privileges

No <InternalLink path="security-reference/authorization#managing-privileges">privileges</InternalLink> are required to reset a session setting.

#### Parameters

| Parameter     | Description                                                                                        |
| ------------- | -------------------------------------------------------------------------------------------------- |
| `session_var` | The name of the <InternalLink path="set-vars#supported-variables">session variable</InternalLink>. |

For usage, see [Synopsis](#synopsis).

#### Aliases

In CockroachDB, the following are aliases for `ALTER DATABASE ... RESET {session variable}`:

* <InternalLink path="alter-role#set-default-session-variable-values-for-all-users">`ALTER ROLE ALL IN DATABASE ... RESET {session variable}`</InternalLink>

### `SET {session variable}`

`ALTER DATABASE ... SET {session variable}` sets the default value of a <InternalLink path="set-vars#supported-variables">session variable</InternalLink> for all future sessions on the database. This command does **not** alter the session setting of the current session.

#### Required privileges

To set the `role` 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 <InternalLink path="security-reference/authorization#managing-privileges">privileges</InternalLink> to modify.

#### Parameters

| Parameter  | Description                                                                                                                                      |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `variable` | The name of <InternalLink path="set-vars#supported-variables">the session variable</InternalLink> to set. The variable name is case-insensitive. |
| `value`    | The value, or list of values, to assign to the session variable.                                                                                 |

For usage, see [Synopsis](#synopsis).

#### Aliases

In CockroachDB, the following are aliases for `ALTER DATABASE ... SET {session variable}`:

* `ALTER ROLE ALL IN DATABASE ... SET {session variable}`

For more information, refer to <InternalLink path="alter-role#set-default-session-variable-values-for-all-users">`ALTER ROLE ALL ...`</InternalLink>.

### `SET PRIMARY REGION`

`ALTER DATABASE .. SET PRIMARY REGION` sets the primary <InternalLink path="multiregion-overview#database-regions">region</InternalLink> of a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.

<Danger>
  If a database's <InternalLink path="configure-replication-zones">zone configuration</InternalLink> has been directly set with an [`ALTER DATABASE ... CONFIGURE ZONE`](#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`](#remove-a-replication-zone) statement.
</Danger>

<InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink> allow you to define a <InternalLink path="multiregion-overview#database-regions">database region</InternalLink> that will be used for failover in the event your primary region goes down. For more information, see <InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink>.

For examples, see [Manage regions](#manage-regions).

#### 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 <InternalLink path="security-reference/authorization#roles">`admin`</InternalLink> role for the cluster.
* Membership to the <InternalLink path="security-reference/authorization#object-ownership">owner</InternalLink> role, or the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink>, for the database and all tables in the database.

To switch primary regions to a region that has already been added to a database, the user must have membership to the <InternalLink path="security-reference/authorization#object-ownership">owner</InternalLink> role for the database, or have the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink> on the database.

#### Parameters

| Parameter     | Description                                                                                                                                                                         |
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `region_name` | The region to set as the database's primary region.<br />Allowed values include any region present in <InternalLink path="show-regions">`SHOW REGIONS FROM CLUSTER`</InternalLink>. |

For usage, see [Synopsis](#synopsis).

### `SET SECONDARY REGION`

`ALTER DATABASE .. SET SECONDARY REGION` adds a <InternalLink path="multiregion-overview#database-regions">secondary region</InternalLink> to a <InternalLink path="multiregion-overview">multi-region database</InternalLink> for failover purposes.

If the [primary region](#set-primary-region) fails, the secondary region becomes the new primary region. For more information, see <InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink>.

<Danger>
  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`](#set-primary-region), or when <InternalLink path="create-database">creating the database</InternalLink>. For an example showing how to add a secondary region with `ALTER DATABASE`, see [Set the secondary region](#set-the-secondary-region).
</Danger>

<InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink> are not compatible with databases containing <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink> 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](#manage-regions).

#### Required privileges

To add a secondary region to a database, the user must have one of the following:

* Membership to the <InternalLink path="security-reference/authorization#admin-role">`admin`</InternalLink> role for the cluster.
* Either <InternalLink path="security-reference/authorization#object-ownership">ownership</InternalLink> or the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink> for the database.

#### Parameters

| Parameter     | Description                                                                                                                                                                                                                                             |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `region_name` | Usually, the <InternalLink path="multiregion-overview#database-regions">region</InternalLink> 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`. |

For usage, see [Synopsis](#synopsis).

### `SURVIVE {ZONE,REGION} FAILURE`

`ALTER DATABASE ... SURVIVE {ZONE,REGION} FAILURE` <InternalLink path="sql-statements">statement</InternalLink> sets the <InternalLink path="multiregion-overview#survival-goals">survival goal</InternalLink> for a <InternalLink path="multiregion-overview">multi-region database</InternalLink>.

For examples, see [Configure survival goals](#configure-survival-goals).

#### Required privileges

The user must be a member of the <InternalLink path="security-reference/authorization#roles">`admin`</InternalLink> or <InternalLink path="security-reference/authorization#object-ownership">owner</InternalLink> roles, or have the <InternalLink path="security-reference/authorization#supported-privileges">`CREATE` privilege</InternalLink> on the database.

#### Parameters

| Parameter | Description                                                                                                      |
| --------- | ---------------------------------------------------------------------------------------------------------------- |
| `ZONE`    | Set the <InternalLink path="multiregion-overview#survival-goals">survival goal</InternalLink> to zone failure.   |
| `REGION`  | Set the <InternalLink path="multiregion-overview#survival-goals">survival goal</InternalLink> to region failure. |

For usage, see [Synopsis](#synopsis).

## Examples

### Manage regions

#### Setup

Only a <InternalLink path="multiregion-overview#cluster-regions">cluster region</InternalLink> specified <InternalLink path="cockroach-start#locality">at node startup</InternalLink> can be used as a <InternalLink path="multiregion-overview#database-regions">database region</InternalLink>.

To follow along with the examples in this section, start a <InternalLink path="cockroach-demo">demo cluster</InternalLink> with the <InternalLink path="cockroach-demo#general">`--global` flag</InternalLink> to simulate a multi-region cluster:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach demo --global --nodes 9
```

To see the regions available to the databases in the cluster, use a <InternalLink path="show-regions#view-the-regions-in-a-cluster">`SHOW REGIONS FROM CLUSTER`</InternalLink> statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SHOW REGIONS FROM CLUSTER;
```

```
     region    |  zones
---------------+----------
  europe-west1 | {b,c,d}
  us-east1     | {b,c,d}
  us-west1     | {a,b,c}
(3 rows)
```

#### Set the primary region

Suppose you want to make the database `movr` 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`](#set-primary-region) statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr SET PRIMARY REGION "us-east1";
```

```
ALTER DATABASE PRIMARY REGION
```

Given a cluster with multiple regions, any databases in that cluster that have not yet had their primary regions set will have their replicas spread as broadly as possible for resiliency. When a primary region is added to one of these databases:

* All tables will be <InternalLink path="alter-table">`REGIONAL BY TABLE`</InternalLink> 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 <InternalLink path="architecture/replication-layer#leaseholder-rebalancing">rebalancing</InternalLink>.

#### Add regions to a database

To add more regions to a database that already has at least one region:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ADD region "us-west1";
```

```
ALTER DATABASE ADD REGION
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ADD region "europe-west1";
```

```
ALTER DATABASE ADD REGION
```

To view the regions associated with a multi-region database, use a <InternalLink path="show-regions">`SHOW REGIONS FROM DATABASE`</InternalLink> statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SHOW REGIONS FROM DATABASE movr;
```

```
  database |    region    | primary | secondary |  zones
-----------+--------------+---------+-----------+----------
  movr     | us-east1     |    t    |     f     | {b,c,d}
  movr     | europe-west1 |    f    |     f     | {b,c,d}
  movr     | us-west1     |    f    |     f     | {a,b,c}
(3 rows)
```

#### Set the secondary region

To set an existing <InternalLink path="multiregion-overview#database-regions">database region</InternalLink> (that is not already the [primary region](#set-the-primary-region)) as the <InternalLink path="multiregion-overview#secondary-regions">secondary region</InternalLink>, use the following statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr SET SECONDARY REGION "us-west1";
```

```
ALTER DATABASE SET SECONDARY REGION
```

Now, the `"us-west1"` region will act as the primary region if the original primary region fails.

<InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink> are not compatible with databases containing <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink> 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 <InternalLink path="multiregion-overview#database-regions">database region</InternalLink> as the <InternalLink path="multiregion-overview#secondary-regions">secondary region</InternalLink> on a multi-region database, use the [`DROP SECONDARY REGION`](#drop-secondary-region) statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr DROP SECONDARY REGION;
```

```
ALTER DATABASE DROP SECONDARY REGION
```

<Note>
  This statement does not drop the region from the database. To drop a region, use [`DROP REGION`](#drop-a-region-from-a-database).
</Note>

#### Change the existing primary region

To change the primary region to another region in the database, use a [`SET PRIMARY REGION`](#set-primary-region) statement.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr SET PRIMARY REGION "europe-west1";
```

```
ALTER DATABASE PRIMARY REGION
```

You can only change an existing primary region to a region that has already been [added to the database](#add-regions-to-a-database). If you try to change the primary region to a region that is not already associated with a database, CockroachDB will return an error.

#### Drop a region from a database

To [drop a region](#drop-region) from a multi-region database, use a [`DROP REGION`](#drop-region) statement.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr DROP REGION "us-east1";
```

```
ALTER DATABASE DROP REGION
```

You cannot drop a region from a multi-region database if:

* The region is [set as the primary region](#set-the-primary-region), unless it is the last remaining region.
* The region is [set as the secondary region](#set-the-secondary-region). To drop the region, you must first unset the secondary region using [`DROP SECONDARY REGION`](#unset-the-secondary-region).
* The database uses the <InternalLink path="multiregion-survival-goals#survive-region-failures">`REGION` survival goal</InternalLink> and there are only three regions configured on the database.

### Manage super regions

#### Setup

Only a <InternalLink path="multiregion-overview#cluster-regions">cluster region</InternalLink> specified <InternalLink path="cockroach-start#locality">at node startup</InternalLink> can be used as a <InternalLink path="multiregion-overview#database-regions">database region</InternalLink>.

To follow along with the examples in this section, start a <InternalLink path="cockroach-demo">demo cluster</InternalLink> with the <InternalLink path="cockroach-demo#general">`--global` flag</InternalLink> to simulate a multi-region cluster:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach demo --global --nodes 9
```

To see the regions available to the databases in the cluster, use a <InternalLink path="show-regions#view-the-regions-in-a-cluster">`SHOW REGIONS FROM CLUSTER`</InternalLink> statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SHOW REGIONS FROM CLUSTER;
```

```
     region    |  zones
---------------+----------
  europe-west1 | {b,c,d}
  us-east1     | {b,c,d}
  us-west1     | {a,b,c}
(3 rows)
```

##### 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 <InternalLink path="multiregion-overview#database-regions">Database Regions</InternalLink>.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr PRIMARY REGION "us-east1";
ALTER DATABASE movr ADD REGION "europe-west1";
ALTER DATABASE movr ADD REGION "us-west1";
```

##### Set up MovR global tables

Because the data in `promo_codes` is not updated frequently (a.k.a., "read-mostly"), and needs to be available from any region, the right table locality is <InternalLink path="table-localities#global-tables">`GLOBAL`</InternalLink>.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER TABLE promo_codes SET locality GLOBAL;
```

Next, alter the `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.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER TABLE user_promo_codes
  ADD CONSTRAINT user_promo_codes_code_fk
    FOREIGN KEY (code)
    REFERENCES promo_codes (code)
    ON UPDATE CASCADE;
```

##### Set up MovR regional tables

All of the tables except `promo_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 <InternalLink path="table-localities#regional-by-row-tables">`REGIONAL BY ROW`</InternalLink>.

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`

  ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
  ALTER TABLE rides ADD COLUMN region crdb_internal_region AS (
    CASE WHEN city = 'amsterdam' THEN 'europe-west1'
         WHEN city = 'paris' THEN 'europe-west1'
         WHEN city = 'rome' THEN 'europe-west1'
         WHEN city = 'new york' THEN 'us-east1'
         WHEN city = 'boston' THEN 'us-east1'
         WHEN city = 'washington dc' THEN 'us-east1'
         WHEN city = 'san francisco' THEN 'us-west1'
         WHEN city = 'seattle' THEN 'us-west1'
         WHEN city = 'los angeles' THEN 'us-west1'
    END
  ) STORED;
  ALTER TABLE rides ALTER COLUMN REGION SET NOT NULL;
  ALTER TABLE rides SET LOCALITY REGIONAL BY ROW AS "region";
  ```

* `user_promo_codes`

  ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
  ALTER TABLE user_promo_codes ADD COLUMN region crdb_internal_region AS (
    CASE WHEN city = 'amsterdam' THEN 'europe-west1'
         WHEN city = 'paris' THEN 'europe-west1'
         WHEN city = 'rome' THEN 'europe-west1'
         WHEN city = 'new york' THEN 'us-east1'
         WHEN city = 'boston' THEN 'us-east1'
         WHEN city = 'washington dc' THEN 'us-east1'
         WHEN city = 'san francisco' THEN 'us-west1'
         WHEN city = 'seattle' THEN 'us-west1'
         WHEN city = 'los angeles' THEN 'us-west1'
    END
  ) STORED;
  ALTER TABLE user_promo_codes ALTER COLUMN REGION SET NOT NULL;
  ALTER TABLE user_promo_codes SET LOCALITY REGIONAL BY ROW AS "region";
  ```

* `users`

  ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
  ALTER TABLE users ADD COLUMN region crdb_internal_region AS (
    CASE WHEN city = 'amsterdam' THEN 'europe-west1'
         WHEN city = 'paris' THEN 'europe-west1'
         WHEN city = 'rome' THEN 'europe-west1'
         WHEN city = 'new york' THEN 'us-east1'
         WHEN city = 'boston' THEN 'us-east1'
         WHEN city = 'washington dc' THEN 'us-east1'
         WHEN city = 'san francisco' THEN 'us-west1'
         WHEN city = 'seattle' THEN 'us-west1'
         WHEN city = 'los angeles' THEN 'us-west1'
    END
  ) STORED;
  ALTER TABLE users ALTER COLUMN REGION SET NOT NULL;
  ALTER TABLE users SET LOCALITY REGIONAL BY ROW AS "region";
  ```

* `vehicle_location_histories`

  ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
  ALTER TABLE vehicle_location_histories ADD COLUMN region crdb_internal_region AS (
    CASE WHEN city = 'amsterdam' THEN 'europe-west1'
         WHEN city = 'paris' THEN 'europe-west1'
         WHEN city = 'rome' THEN 'europe-west1'
         WHEN city = 'new york' THEN 'us-east1'
         WHEN city = 'boston' THEN 'us-east1'
         WHEN city = 'washington dc' THEN 'us-east1'
         WHEN city = 'san francisco' THEN 'us-west1'
         WHEN city = 'seattle' THEN 'us-west1'
         WHEN city = 'los angeles' THEN 'us-west1'
    END
  ) STORED;
  ALTER TABLE vehicle_location_histories ALTER COLUMN REGION SET NOT NULL;
  ALTER TABLE vehicle_location_histories SET LOCALITY REGIONAL BY ROW AS "region";
  ```

* `vehicles`

  ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
  ALTER TABLE vehicles ADD COLUMN region crdb_internal_region AS (
    CASE WHEN city = 'amsterdam' THEN 'europe-west1'
         WHEN city = 'paris' THEN 'europe-west1'
         WHEN city = 'rome' THEN 'europe-west1'
         WHEN city = 'new york' THEN 'us-east1'
         WHEN city = 'boston' THEN 'us-east1'
         WHEN city = 'washington dc' THEN 'us-east1'
         WHEN city = 'san francisco' THEN 'us-west1'
         WHEN city = 'seattle' THEN 'us-west1'
         WHEN city = 'los angeles' THEN 'us-west1'
    END
  ) STORED;
  ALTER TABLE vehicles ALTER COLUMN REGION SET NOT NULL;
  ALTER TABLE vehicles SET LOCALITY REGIONAL BY ROW AS "region";
  ```

#### Enable super regions

To enable super regions, set the `enable_super_regions` <InternalLink path="set-vars">session setting</InternalLink> to `'on'`:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SET enable_super_regions = 'on';
```

```
SET
```

You can also set the `sql.defaults.super_regions.enabled` <InternalLink path="cluster-settings">cluster setting</InternalLink> to `true`:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SET CLUSTER SETTING  sql.defaults.super_regions.enabled = true;
```

```
SET CLUSTER SETTING
```

#### Add a super region to a database

To add a super region to a multi-region database, use the `ALTER DATABASE ... ADD SUPER REGION` statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ADD SUPER REGION "usa" VALUES "us-east1", "us-west1";
```

```
ALTER DATABASE ADD SUPER REGION
```

#### 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](#add-a-super-region-to-a-database). If you wanted to [drop the region](#drop-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:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ALTER SUPER REGION "usa" VALUES "us-east1";
```

```
ALTER DATABASE ALTER SUPER REGION
```

To add a region to a super region, alter the super region as shown above to be a list of regions that includes the existing and the new regions.

#### Allow user to modify a primary region that is part of a super region

By default, you may not change the <InternalLink path="alter-database#set-primary-region">primary region</InternalLink> of a <InternalLink path="multiregion-overview">multi-region database</InternalLink> 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 <InternalLink path="regional-tables">regional table</InternalLink> 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 the `alter_primary_region_super_region_override` <InternalLink path="set-vars">session setting</InternalLink> to `'on'`:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SET alter_primary_region_super_region_override = 'on';
```

```
SET
```

You can also accomplish this by setting the `sql.defaults.alter_primary_region_super_region_override.enable` <InternalLink path="cluster-settings">cluster setting</InternalLink> to `true`:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SET CLUSTER SETTING  sql.defaults.alter_primary_region_super_region_override.enable = true;
```

```
SET CLUSTER SETTING
```

#### Drop a super region from a database

To drop a super region from a multi-region database, use a [`DROP SUPER REGION`](#drop-super-region) statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr DROP SUPER REGION "usa";
```

```
ALTER DATABASE DROP SUPER REGION
```

Note that you cannot [drop a region](#drop-region) that is part of a super region until you either [alter the super region](#alter-super-region) to remove it, or [drop the super region](#drop-super-region) altogether.

For example, using the super region that was added in [`ADD SUPER REGION`](#add-a-super-region-to-a-database):

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr DROP REGION "us-west1";
```

```
ERROR: region us-west1 is part of super region usa
SQLSTATE: 2BP01
HINT: you must first drop super region usa before you can drop the region us-west1
```

### Configure replication zones

Cockroach Labs  <InternalLink path="configure-replication-zones#why-manual-zone-config-management-is-not-recommended">does not recommend modifying zone configurations manually</InternalLink>.  [does not recommend modifying zone configurations manually](#why-manual-zone-config-management-is-not-recommended).

Most users should use <InternalLink path="multiregion-overview">multi-region SQL statements</InternalLink> instead. If additional control is needed, use <InternalLink path="zone-config-extensions">Zone Config Extensions</InternalLink> 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 <InternalLink path="movr">MovR: A Global Vehicle-sharing App</InternalLink>.

To follow along, run <InternalLink path="cockroach-demo">`cockroach demo`</InternalLink> 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.

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach demo --geo-partitioned-replicas
```

#### Create a replication zone for a database

To control replication for a specific database, use the `ALTER DATABASE ... CONFIGURE ZONE` statement to define the relevant values (other values will be inherited from the parent zone):

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> ALTER DATABASE movr CONFIGURE ZONE USING num_replicas = 5, gc.ttlseconds = 100000;
```

```
CONFIGURE ZONE 1
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SHOW ZONE CONFIGURATION FROM DATABASE movr;
```

```
     target     |              raw_config_sql
----------------+-------------------------------------------
  DATABASE movr | ALTER DATABASE movr CONFIGURE ZONE USING
                |     range_min_bytes = 134217728,
                |     range_max_bytes = 536870912,
                |     gc.ttlseconds = 100000,
                |     num_replicas = 5,
                |     constraints = '[]',
                |     lease_preferences = '[]'
(1 row)
```

#### Edit a replication zone

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr CONFIGURE ZONE USING range_min_bytes = 0, range_max_bytes = 90000, gc.ttlseconds = 89999, num_replicas = 4;
```

#### Remove a replication zone

<Note>
  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 <InternalLink path="migrate-to-multiregion-sql#replication-zone-patterns-and-multi-region-sql-abstractions">multi-region abstractions</InternalLink> created the zone configuration.
</Note>

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr CONFIGURE ZONE DISCARD;
```

### Troubleshoot replication zones

For instructions showing how to troubleshoot replication zones that may be misconfigured, see <InternalLink path="troubleshoot-replication-zones">Troubleshoot Replication Zone Configurations</InternalLink>.

### Use Zone Config Extensions

The following examples show:

* How to [override specific fields of a schema object's zone configs](#override-specific-fields-of-a-schema-object’s-zone-configs).
* How something like the <InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink> multi-region abstraction could have been implemented using Zone Config Extensions. For this example, we will call it ["Failover regions"](#failover-regions).
* How to [reset a region's Zone Config Extensions](#reset-a-region’s-zone-config-extensions).
* How to [discard a region's Zone Config Extensions](#discard-a-region’s-zone-config-extensions).
* How you could [implement super regions](#implement-super-regions).
* How to [minimize cross-region write latency](#minimize-cross-region-write-latency).

<Note>
  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.
</Note>

#### Setup

The setup described in this section will be used in the following examples.

##### Start a cluster

Start a <InternalLink path="cockroach-demo">`cockroach demo`</InternalLink> cluster as follows:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
cockroach demo --global --nodes=9
```

This gives us a (preloaded) <InternalLink path="movr">MovR</InternalLink> database on a cluster with the following <InternalLink path="multiregion-overview#cluster-regions">regions</InternalLink>, which can be viewed with <InternalLink path="show-regions">`SHOW REGIONS`</InternalLink>:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SHOW REGIONS;
```

```
     region    |  zones  | database_names | primary_region_of | secondary_region_of
---------------+---------+----------------+-------------------+----------------------
  europe-west1 | {b,c,d} | {}             | {}                | {}
  us-east1     | {b,c,d} | {}             | {}                | {}
  us-west1     | {a,b,c} | {}             | {}                | {}
(3 rows)
```

##### Make the database multi-region

Next, modify the database to use the <InternalLink path="multiregion-overview">multi-region abstractions</InternalLink> as follows:

1. Set the primary region using [`SET PRIMARY REGION`](#set-primary-region).
2. Add the other two regions using [`ADD REGION`](#add-region).

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr SET PRIMARY REGION "us-east1";
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ADD REGION "us-west1";
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ADD REGION "europe-west1";
```

#### Override specific fields of a schema object's zone configs

In this example we will configure a multi-region <InternalLink path="movr">MovR</InternalLink> database to update its <InternalLink path="configure-replication-zones">`lease_preferences`</InternalLink> field.

Setting the [primary region](#set-primary-region) to `us-east1` [during the setup steps](#use-zone-config-extensions) added `us-east1` to `lease_preferences`.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SHOW ZONE CONFIGURATION FROM DATABASE movr;
```

```
     target     |                                      raw_config_sql
----------------+-------------------------------------------------------------------------------------------
  DATABASE movr | ALTER DATABASE movr CONFIGURE ZONE USING
                |     range_min_bytes = 134217728,
                |     range_max_bytes = 536870912,
                |     gc.ttlseconds = 90000,
                |     num_replicas = 5,
                |     num_voters = 3,
                |     constraints = '{+region=europe-west1: 1, +region=us-east1: 1, +region=us-west1: 1}',
                |     voter_constraints = '[+region=us-east1]',
                |     lease_preferences = '[[+region=us-east1]]'
(1 row)
```

We will now use `ALTER DATABASE ... ALTER LOCALITY` to overwrite the `lease_preferences` field to add `us-west1` to the list of regions:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ALTER LOCALITY REGIONAL IN "us-east1" CONFIGURE ZONE USING lease_preferences = '[[+region=us-east1], [+region=us-west1]]';
```

To view the updated zone configs, enter the following statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SHOW ZONE CONFIGURATION FROM DATABASE movr;
```

```
     target     |                                      raw_config_sql
----------------+-------------------------------------------------------------------------------------------
  DATABASE movr | ALTER DATABASE movr CONFIGURE ZONE USING
                |     range_min_bytes = 134217728,
                |     range_max_bytes = 536870912,
                |     gc.ttlseconds = 90000,
                |     num_replicas = 5,
                |     num_voters = 3,
                |     constraints = '{+region=europe-west1: 1, +region=us-east1: 1, +region=us-west1: 1}',
                |     voter_constraints = '[+region=us-east1]',
                |     lease_preferences = '[[+region=us-east1], [+region=us-west1]]'
(1 row)
```

The `lease_preferences` field is now updated to include `us-west1`.

To remove the changes made in this example, [reset the Zone Config Extensions](#reset-a-region’s-zone-config-extensions).

#### Implement super regions

In this example, <InternalLink path="zone-config-extensions">Zone Config Extensions</InternalLink> are used to provide an alternative implementation of <InternalLink path="multiregion-overview#super-regions">super regions</InternalLink>, which are useful for <InternalLink path="data-domiciling">data domiciling</InternalLink>.

For this example, you need to start a <InternalLink path="cockroach-demo">`cockroach demo` cluster</InternalLink> using the following command, which <InternalLink path="cockroach-demo">defines the available cluster localities using the `--demo-locality` flag</InternalLink>:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
cockroach demo --nodes=9 --demo-locality=region=us-east1:region=us-east1:region=us-central1:region=us-west1:region=europe-west1:region=europe-west1:region=europe-central1:region=europe-east1:region=europe-east1
```

Next, enter the following statements at the SQL prompt. These statements will:

* Make the <InternalLink path="movr">`movr` database</InternalLink> into a <InternalLink path="multiregion-overview">multi-region database</InternalLink> by <InternalLink path="alter-database#set-primary-region">setting the primary region</InternalLink>.
* Set the `movr` database to <InternalLink path="multiregion-survival-goals#survive-region-failures">survive region failures</InternalLink>.
* Add the remaining regions defined at cluster start time to the multi-region `movr` database (for a total of 6).

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr SET PRIMARY REGION "us-east1";
ALTER DATABASE movr ADD REGION "us-central1";
ALTER DATABASE movr ADD REGION "us-west1";
ALTER DATABASE movr ADD REGION "europe-west1";
ALTER DATABASE movr ADD REGION "europe-central1";
ALTER DATABASE movr ADD REGION "europe-east1";
ALTER DATABASE movr SURVIVE REGION FAILURE;
```

Now that the multi-region cluster is configured, we will use the following [`ALTER LOCALITY`](#alter-locality) statements to <InternalLink path="configure-replication-zones">configure replication zones</InternalLink> such that:

* <InternalLink path="architecture/overview">Replicas</InternalLink> in the `us-east1` region are only stored on nodes in the US.
* Replicas in the `europe-central1` region are only stored on nodes in the EU.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ALTER LOCALITY REGIONAL IN "us-east1" CONFIGURE ZONE USING
    num_replicas = 7,
    constraints = '{+region=us-east1: 3,
                    +region=us-central1: 3,
                    +region=us-west1: 1}',
    lease_preferences = '[[+region=us-east1], [+region=us-central1]]';
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ALTER LOCALITY REGIONAL IN "europe-central1" CONFIGURE ZONE USING
    num_replicas = 7,
    constraints = '{+region=europe-central1: 3,
                    +region=europe-west1: 3,
                    +region=europe-east1: 1}',
    lease_preferences = '[[+region=europe-central1], [+region=europe-west1]]';
```

#### Minimize cross-region write latency

In this example, <InternalLink path="zone-config-extensions">Zone Config Extensions</InternalLink> 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 <InternalLink path="follower-reads">follower reads</InternalLink> will be made from these regions.

To minimize the cross-region write latency, <InternalLink path="configure-replication-zones">configure replication zones</InternalLink> such that the 3 US regions have voting replicas (that is, replicas that participate in <InternalLink path="architecture/replication-layer#raft">Raft quorum</InternalLink>). This will leave the 2 additional non-US regions with only <InternalLink path="architecture/replication-layer#non-voting-replicas">non-voting replicas</InternalLink>.

For this example, you need to start a <InternalLink path="cockroach-demo">`cockroach demo` cluster</InternalLink> using the following command, which <InternalLink path="cockroach-demo">defines the available cluster localities using the `--demo-locality` flag</InternalLink>:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
cockroach demo --nodes=5 --demo-locality=region=us-east1:region=us-west1:region=us-central1:region=europe-west1:region=asia-northeast1
```

Next, enter the following statements at the SQL prompt. These statements will:

* Make the <InternalLink path="movr">`movr` database</InternalLink> into a <InternalLink path="multiregion-overview">multi-region database</InternalLink> by <InternalLink path="alter-database#set-primary-region">setting the primary region</InternalLink>.
* Set the `movr` database to <InternalLink path="multiregion-survival-goals#survive-region-failures">survive region failures</InternalLink>.
* Add the remaining regions defined at cluster start time to the multi-region `movr` database (for a total of 5).

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr SET PRIMARY REGION "us-west1";
ALTER DATABASE movr ADD REGION "us-central1";
ALTER DATABASE movr ADD REGION "us-east1";
ALTER DATABASE movr ADD REGION "europe-west1";
ALTER DATABASE movr ADD REGION "asia-northeast1";
ALTER DATABASE movr SURVIVE REGION FAILURE;
```

By default, all tables in the `movr` database are <InternalLink path="regional-tables">`REGIONAL` tables</InternalLink>:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SHOW TABLES;
```

```
  schema_name |         table_name         | type  | owner | estimated_row_count |              locality
--------------+----------------------------+-------+-------+---------------------+--------------------------------------
  public      | promo_codes                | table | demo  |                1000 | REGIONAL BY TABLE IN PRIMARY REGION
  public      | rides                      | table | demo  |                 500 | REGIONAL BY TABLE IN PRIMARY REGION
  public      | user_promo_codes           | table | demo  |                   5 | REGIONAL BY TABLE IN PRIMARY REGION
  public      | users                      | table | demo  |                  50 | REGIONAL BY TABLE IN PRIMARY REGION
  public      | vehicle_location_histories | table | demo  |                1000 | REGIONAL BY TABLE IN PRIMARY REGION
  public      | vehicles                   | table | demo  |                  15 | REGIONAL BY TABLE IN PRIMARY REGION
(6 rows)
```

Now that the multi-region cluster is configured, we will use the following [`ALTER LOCALITY`](#alter-locality) statements to <InternalLink path="configure-replication-zones">configure replication zones</InternalLink> such that:

* The 3 US regions have voting replicas (that is, replicas that participate in <InternalLink path="architecture/replication-layer#raft">Raft quorum</InternalLink>).
* The 2 additional regions (Europe and Japan) are left with only <InternalLink path="architecture/replication-layer#non-voting-replicas">non-voting replicas</InternalLink> that can be used to serve <InternalLink path="follower-reads">follower reads</InternalLink>.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ALTER LOCALITY REGIONAL CONFIGURE ZONE USING voter_constraints = '{+region=us-west1: 2, +region=us-central1: 2, +region=us-east1: 1}';
```

#### Failover regions

In this example we will use Zone Config Extensions to configure a multi-region <InternalLink path="movr">MovR</InternalLink> database so that if the [primary region](#set-primary-region) fails, enough <InternalLink path="architecture/distribution-layer">replicas</InternalLink> 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 be `REGIONAL IN`](#parameters), which covers all <InternalLink path="alter-table#set-the-table-locality-to-regional-by-table">`REGIONAL BY TABLE IN {region}` tables</InternalLink> and all <code>{'{region}'}</code> <InternalLink path="partitioning">partitions</InternalLink> of <InternalLink path="alter-table#set-the-table-locality-to-regional-by-row">`REGIONAL BY ROW` tables</InternalLink>.

<Note>
  This functionality is already provided by the built-in <InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink> feature. It is used here to show the flexibility of Zone Config Extensions. We strongly recommend using the <InternalLink path="multiregion-overview">built-in multi-region features</InternalLink> whenever possible.
</Note>

1. Set the database to have a `REGION` survival goal using <InternalLink path="alter-database">`ALTER DATABASE ... SURVIVE REGION FAILURE`</InternalLink>:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   ALTER DATABASE movr SURVIVE REGION FAILURE;
   ```

2. Apply the `REGIONAL BY ROW` locality to the `movr.rides` table using the following statement:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   ALTER TABLE rides ADD COLUMN region crdb_internal_region AS (
    CASE WHEN city = 'amsterdam' THEN 'europe-west1'
         WHEN city = 'paris' THEN 'europe-west1'
         WHEN city = 'rome' THEN 'europe-west1'
         WHEN city = 'new york' THEN 'us-east1'
         WHEN city = 'boston' THEN 'us-east1'
         WHEN city = 'washington dc' THEN 'us-east1'
         WHEN city = 'san francisco' THEN 'us-west1'
         WHEN city = 'seattle' THEN 'us-west1'
         WHEN city = 'los angeles' THEN 'us-west1'
    END
   ) STORED;
   ALTER TABLE rides ALTER COLUMN region SET NOT NULL;
   ALTER TABLE rides SET LOCALITY REGIONAL BY ROW AS "region";
   ```

3. View the <InternalLink path="configure-replication-zones">zone configs</InternalLink> for the `movr.rides` table using <InternalLink path="show-zone-configurations">`SHOW ZONE CONFIGURATION`</InternalLink>:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   SHOW ZONE CONFIGURATION FROM TABLE movr.rides;
   ```

   ```
        target     |                                      raw_config_sql
   ----------------+-------------------------------------------------------------------------------------------
     DATABASE movr | ALTER DATABASE movr CONFIGURE ZONE USING
                   |     range_min_bytes = 134217728,
                   |     range_max_bytes = 536870912,
                   |     gc.ttlseconds = 90000,
                   |     num_replicas = 5,
                   |     num_voters = 5,
                   |     constraints = '{+region=europe-west1: 1, +region=us-east1: 1, +region=us-west1: 1}',
                   |     voter_constraints = '{+region=us-east1: 2}',
                   |     lease_preferences = '[[+region=us-east1]]'
   (1 row)
   ```

   Remember that [we configured `us-east1` to be our primary region during cluster setup](#use-zone-config-extensions). The output above confirms that `us-east1` is the primary region based on the values of the <InternalLink path="configure-replication-zones">`voter_constraints`</InternalLink> and <InternalLink path="configure-replication-zones">`lease_preferences`</InternalLink> keys.

4. Update the configuration to keep additional voting replicas and leaseholders in `us-west1`. We do this because we would like to configure `us-west1` to be the failover region for `us-east1`. The following SQL statement accomplishes this by configuring `us-east1` to keep additional voting replicas and leaseholders in `us-west1`. This means that if `us-east1` fails, it will fail over to `us-west1`.

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   ALTER DATABASE movr ALTER LOCALITY REGIONAL IN "us-east1" CONFIGURE ZONE USING voter_constraints = '{+region=us-east1: 2, +region=us-west1: 2}', lease_preferences = '[[+region=us-east1], [+region=us-west1]]';
   ```

5. View the <InternalLink path="configure-replication-zones">zone configs</InternalLink> for the `movr.rides` table using <InternalLink path="show-zone-configurations">`SHOW ZONE CONFIGURATION`</InternalLink>:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   SHOW ZONE CONFIGURATION FROM TABLE movr.rides;
   ```

   ```
        target     |                                      raw_config_sql
   ----------------+-------------------------------------------------------------------------------------------
     DATABASE movr | ALTER DATABASE movr CONFIGURE ZONE USING
                   |     range_min_bytes = 134217728,
                   |     range_max_bytes = 536870912,
                   |     gc.ttlseconds = 90000,
                   |     num_replicas = 5,
                   |     num_voters = 5,
                   |     constraints = '{+region=europe-west1: 1, +region=us-east1: 1, +region=us-west1: 1}',
                   |     voter_constraints = '{+region=us-east1: 2, +region=us-west1: 2}',
                   |     lease_preferences = '[[+region=us-east1], [+region=us-west1]]'
   (1 row)
   ```

   The following changes are shown:

   * There are now 2 voting replicas stored in `us-west1`.
   * There is now a preference that if leases cannot be placed in `us-east1`, they should be placed in `us-west1`.

   Both of these changes combine to ensure that if `us-east1` goes down, the cluster will still be able to operate until some mitigation is in place.

To remove the zone config changes made in this example, [reset the Zone Config Extensions](#reset-a-region’s-zone-config-extensions).

#### 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:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ALTER LOCALITY CONFIGURE ZONE USING DEFAULT;
```

<Note>
  This will not reset any configuration created by the <InternalLink path="multiregion-overview">multi-region abstractions</InternalLink>.
</Note>

#### Discard a region's Zone Config Extensions

To discard the Zone Config Extension settings from a region, enter the following statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr ALTER LOCALITY CONFIGURE ZONE DISCARD;
```

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.

However, this statement will not remove any configuration created by the <InternalLink path="multiregion-overview">multi-region abstractions</InternalLink>.

#### 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 <InternalLink path="troubleshoot-replication-zones">Troubleshoot Replication Zone Configurations</InternalLink>.

### Change database owner

#### 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 <InternalLink path="movr">MovR: A Global Vehicle-sharing App</InternalLink>.

To follow along, run <InternalLink path="cockroach-demo">`cockroach demo`</InternalLink> to start a temporary, in-memory cluster with the `movr` dataset preloaded:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach demo
```

#### Change a database's owner

Suppose that the current owner of the `movr` database is `root` and you want to change the owner to a new user named `max`.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> ALTER DATABASE movr OWNER TO max;
```

To verify that the owner is now `max`, query the `pg_catalog.pg_database` and `pg_catalog.pg_roles` tables:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SELECT rolname FROM pg_catalog.pg_database d JOIN pg_catalog.pg_roles r ON d.datdba = r.oid WHERE datname = 'movr';
```

```
  rolname
-----------
  max
(1 row)
```

<Note>
  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` <InternalLink path="security-reference/authorization#managing-privileges">privilege</InternalLink>.
</Note>

### Set replica placement policy

#### Setup

To follow along with the examples below:

1. Start a <InternalLink path="cockroach-demo">demo cluster</InternalLink> with the <InternalLink path="cockroach-demo#general">`--global` flag</InternalLink> to simulate a multi-region cluster:

   ```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   cockroach demo --global --nodes 9
   ```

2. Set the demo cluster's <InternalLink path="multiregion-overview#database-regions">database regions</InternalLink> and <InternalLink path="multiregion-overview">table localities</InternalLink> as described in <InternalLink path="demo-low-latency-multi-region-deployment">Low Latency Reads and Writes in a Multi-Region Cluster</InternalLink> (specifically, starting at <InternalLink path="demo-low-latency-multi-region-deployment#step-5-execute-multi-region-sql-statements">Step 5. Execute multi-region SQL statements</InternalLink>).

3. Enable the replica placement syntax with either the <InternalLink path="set-vars">session variable</InternalLink> or the <InternalLink path="cluster-settings">cluster setting</InternalLink> as shown below.

   1. To use the session variable:

      ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
      SET enable_multiregion_placement_policy = on;
      ```

   2. To use the cluster setting:

      ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
      SET CLUSTER SETTING sql.defaults.multiregion_placement_policy.enabled = on;
      ```

<Note>
  Use <InternalLink path="alter-role#set-default-session-variable-values-for-all-users">`ALTER ROLE ALL SET {sessionvar} = {val}`</InternalLink> instead of the `sql.defaults.*` <InternalLink path="cluster-settings">cluster settings</InternalLink>. This allows you to set a default value for all users for any <InternalLink path="set-vars">session variable</InternalLink> that applies during login, making the `sql.defaults.*` cluster settings redundant.
</Note>

#### 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"](#parameters-restricted), you can do so in a <InternalLink path="create-database">`CREATE DATABASE`</InternalLink> statement as shown below:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
CREATE DATABASE movr2 PRIMARY REGION "us-east1" REGIONS "us-west1", "europe-west1" PLACEMENT RESTRICTED;
```

```
CREATE DATABASE
```

#### Set the replica placement policy to `RESTRICTED`

When you set the database's placement policy to [`RESTRICTED`](#parameters-restricted), you are saying that you want the underlying data to be restricted to the table or partition's <InternalLink path="multiregion-overview#table-localities">home region</InternalLink>.

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr PLACEMENT RESTRICTED;
```

```
ALTER DATABASE PLACEMENT
```

#### Set the replica placement policy to `DEFAULT`

If previously you set the replica placement policy to [`RESTRICTED`](#set-the-replica-placement-policy-to-restricted), you can set it back to [the default](#parameters-default) by issuing the following statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE movr PLACEMENT DEFAULT;
```

```
ALTER DATABASE PLACEMENT
```

### Rename database

#### Rename a database

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CREATE DATABASE db1;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SHOW DATABASES;
```

```
  database_name
-----------------
  db1
  defaultdb
  movr
  postgres
  system
(5 rows)
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> ALTER DATABASE db1 RENAME TO db2;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SHOW DATABASES;
```

```
  database_name
-----------------
  db2
  defaultdb
  movr
  postgres
  system
(5 rows)
```

### Configure survival goals

#### Survive zone failures

To change the survival goal of a multi-region database to survive zone failures, use the following statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE {db} SURVIVE ZONE FAILURE;
```

```
ALTER DATABASE SURVIVE
```

<Note>
  Surviving zone failures is the default setting for multi-region databases.
</Note>

For more information about the zone survival goal, see <InternalLink path="multiregion-survival-goals#survive-zone-failures">Surviving zone failures</InternalLink>.

#### Survive region failures

To change the survival goal of a multi-region database to survive region failures, use the following statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
ALTER DATABASE {db} SURVIVE REGION FAILURE;
```

```
ALTER DATABASE SURVIVE
```

If you try to change a database with less than 3 <InternalLink path="multiregion-overview#database-regions">database regions</InternalLink> to survive region failures, the following error will be signalled:

```
ERROR: at least 3 regions are required for surviving a region failure
SQLSTATE: 42602
HINT: you must add additional regions to the database using ALTER DATABASE mr ADD REGION <region_name>
```

For more information about the region survival goal, see <InternalLink path="multiregion-survival-goals#survive-region-failures">Surviving region failures</InternalLink>.

## See also

* <InternalLink path="multiregion-overview">Multi-Region Capabilities Overview</InternalLink>
* <InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink>
* <InternalLink path="multiregion-overview#super-regions">Super regions</InternalLink>
* <InternalLink path="zone-config-extensions">Zone Config Extensions</InternalLink>
* <InternalLink path="data-domiciling">Data Domiciling with CockroachDB</InternalLink>
* <InternalLink path="create-database">`CREATE DATABASE`</InternalLink>
* <InternalLink path="create-table">`CREATE TABLE`</InternalLink>
* <InternalLink path="alter-table">`ALTER TABLE`</InternalLink>
* <InternalLink path="online-schema-changes">Online Schema Changes</InternalLink>
* <InternalLink path="sql-statements">SQL Statements</InternalLink>
* <InternalLink path="troubleshoot-replication-zones">Troubleshoot Replication Zones</InternalLink>
