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

# CREATE 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 `CREATE DATABASE` <InternalLink path="sql-statements">statement</InternalLink> creates a new CockroachDB database.

<Note>
  The \`\` statement performs a schema change. For more information about how online schema changes work in CockroachDB, see <InternalLink path="online-schema-changes">Online Schema Changes</InternalLink>.
</Note>

## Required privileges

To create 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.

## Synopsis

<img src="https://mintcdn.com/cockroachlabs/9gyQKEP-CuQuCsI3/images/sql-diagrams/v25.3/create_database.svg?fit=max&auto=format&n=9gyQKEP-CuQuCsI3&q=85&s=3b35fe66e9d949d5f6a87b37a3f90c7d" alt="create_database syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="751" height="765" data-path="images/sql-diagrams/v25.3/create_database.svg" />

## Parameters

| Parameter                                                        | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ---------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `IF NOT EXISTS`                                                  | Create a new database only if a database of the same name does not already exist; if one does exist, do not return an error.                                                                                                                                                                                                                                                                                                                                                                                                                |
| `name`                                                           | The name of the database to create, which [must be unique](#create-fails-name-already-in-use) and follow these <InternalLink path="keywords-and-identifiers#identifiers">identifier rules</InternalLink>.<br /><br />Cockroach Labs recommends against starting your database name with the string `cluster:`. If your database name begins with this string, you must append the following to the URI connection string to <InternalLink path="connect-to-the-database">connect to the cluster</InternalLink>: `&options=-ccluster=system` |
| `encoding`                                                       | The `CREATE DATABASE` statement accepts an optional `ENCODING` clause for compatibility with PostgreSQL, but `UTF-8` is the only supported encoding. The aliases `UTF8` and `UNICODE` are also accepted. Values should be enclosed in single quotes and are case-insensitive.<br /><br />Example: `CREATE DATABASE bank ENCODING = 'UTF-8'`.                                                                                                                                                                                                |
| `CONNECTION LIMIT`                                               | Supported for compatibility with PostgreSQL. A value of `-1` indicates no connection limit. Values other than `-1` are currently not supported. By default, `CONNECTION LIMIT = -1`. ([\*](#connlimit-note))                                                                                                                                                                                                                                                                                                                                |
| `PRIMARY REGION region_name`                                     | Create a <InternalLink path="multiregion-overview">multi-region database</InternalLink> with `region_name` as <InternalLink path="multiregion-overview#database-regions">the primary region</InternalLink>.<br />Allowed values include any region returned by <InternalLink path="show-regions">`SHOW REGIONS FROM CLUSTER`</InternalLink>.                                                                                                                                                                                                |
| `REGIONS region_name_list`                                       | Create a <InternalLink path="multiregion-overview">multi-region database</InternalLink> with `region_name_list` as <InternalLink path="multiregion-overview#database-regions">database regions</InternalLink>.<br />Allowed values include any region returned by <InternalLink path="show-regions">`SHOW REGIONS FROM CLUSTER`</InternalLink>.<br />To set database regions at database creation, a primary region must be specified in the same `CREATE DATABASE` statement.                                                              |
| `SURVIVE ZONE FAILURE` (*Default*)<br />`SURVIVE REGION FAILURE` | Create a <InternalLink path="multiregion-overview">multi-region database</InternalLink> with regional failure or zone failure <InternalLink path="multiregion-overview#survival-goals">survival goals</InternalLink>.<br />To set the regional failure survival goal, the database must have at least 3 <InternalLink path="multiregion-overview#database-regions">database regions</InternalLink>.<br />Surviving zone failures is the default setting for multi-region databases.                                                         |

*

To control the maximum number of non-superuser (<InternalLink path="security-reference/authorization">`root`</InternalLink> user or other <InternalLink path="security-reference/authorization#admin-role">`admin` role</InternalLink>) connections a <InternalLink path="architecture/sql-layer">gateway node</InternalLink> can have open at one time, use the `server.max_connections_per_gateway` <InternalLink path="cluster-settings">cluster setting</InternalLink>. If a new non-superuser connection would exceed this limit, the error message `"sorry, too many clients already"` is returned, along with error code `53300`. This setting may be useful until the `CONNECTION LIMIT` syntax is fully supported.

## Example

### Create a database

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

```
CREATE DATABASE
```

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

```
  database_name | owner | primary_region | regions | survival_goal
----------------+-------+----------------+---------+----------------
  bank          | demo  | NULL           | {}      | NULL
  defaultdb     | root  | NULL           | {}      | NULL
  postgres      | root  | NULL           | {}      | NULL
  system        | node  | NULL           | {}      | NULL
(4 rows)
```

### Create fails (name already in use)

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

```
ERROR: database "bank" already exists
SQLSTATE: 42P04
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CREATE DATABASE IF NOT EXISTS bank;
```

```
CREATE DATABASE
```

SQL does not generate an error, but instead responds `CREATE DATABASE` even though a new database wasn't created.

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

```
  database_name | owner | primary_region | regions | survival_goal
----------------+-------+----------------+---------+----------------
  bank          | demo  | NULL           | {}      | NULL
  defaultdb     | root  | NULL           | {}      | NULL
  postgres      | root  | NULL           | {}      | NULL
  system        | node  | NULL           | {}      | NULL
(4 rows)
```

### Create a multi-region database

Suppose you start a cluster with region and zone <InternalLink path="cockroach-start#locality">localities specified at startup</InternalLink>.

For this example, let's use a <InternalLink path="cockroach-demo">demo cluster</InternalLink>, with the <InternalLink path="cockroach-demo#general">`--demo-locality` flag</InternalLink> to simulate a multi-region cluster:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
cockroach demo --nodes=6 --demo-locality=region=us-east1,zone=us-east1-a:region=us-east1,zone=us-east1-b:region=us-central1,zone=us-central1-a:region=us-central1,zone=us-central1-b:region=us-west1,zone=us-west1-a:region=us-west1,zone=us-west1-b --no-example-database
```

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

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

If regions are set at cluster start-up, you can create multi-region databases in the cluster that use the cluster regions.

Use the following command to specify regions and survival goals at database creation:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CREATE DATABASE bank PRIMARY REGION "us-east1" REGIONS "us-east1", "us-central1", "us-west1" SURVIVE REGION FAILURE;
```

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

```
  database_name | owner | primary_region |             regions             | survival_goal
----------------+-------+----------------+---------------------------------+----------------
  bank          | demo  | us-east1       | {us-central1,us-east1,us-west1} | region
  defaultdb     | root  | NULL           | {}                              | NULL
  postgres      | root  | NULL           | {}                              | NULL
  system        | node  | NULL           | {}                              | NULL
(4 rows)
```

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

```
  database |   region    | primary |             zones
-----------+-------------+---------+--------------------------------
  bank     | us-east1    |  true   | {us-east1-a,us-east1-b}
  bank     | us-central1 |  false  | {us-central1-a,us-central1-b}
  bank     | us-west1    |  false  | {us-west1-a,us-west1-b}
(3 rows)
```

### Create a multi-region database with a secondary region

You can add 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 <InternalLink path="alter-database#set-primary-region">primary region</InternalLink> fails, the secondary region becomes the new primary region.

To add a secondary region during database creation, use the following steps:

1. Start a `cockroach demo` cluster as described in the example [Create a multi-region database](#create-a-multi-region-database).

2. Issue a `CREATE DATABASE` statement like the following.  It is the same as in the [Create a multi-region database](#create-a-multi-region-database) example, except that it adds a `SECONDARY REGION {region}` clause:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
CREATE DATABASE bank PRIMARY REGION "us-east1" REGIONS "us-east1", "us-central1", "us-west1" SURVIVE REGION FAILURE SECONDARY REGION "us-west1";
```

```
CREATE DATABASE
```

For more information about secondary regions, see <InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink>.

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

## See also

* <InternalLink path="show-databases">`SHOW DATABASES`</InternalLink>
* <InternalLink path="show-create">`SHOW CREATE DATABASE`</InternalLink>
* <InternalLink path="alter-database#rename-to">`ALTER DATABASE ... RENAME TO`</InternalLink>
* <InternalLink path="set-vars">`SET DATABASE`</InternalLink>
* <InternalLink path="drop-database">`DROP DATABASE`</InternalLink>
* <InternalLink path="sql-statements">SQL Statements</InternalLink>
* <InternalLink path="online-schema-changes">Online Schema Changes</InternalLink>
* <InternalLink path="multiregion-overview">Multiregion overview</InternalLink>
* <InternalLink path="multiregion-overview#secondary-regions">Secondary regions</InternalLink>.
* <InternalLink path="alter-database#set-secondary-region">`SET SECONDARY REGION`</InternalLink>
* <InternalLink path="alter-database#drop-secondary-region">`DROP SECONDARY REGION`</InternalLink>
