Skip to main content
The CREATE DATABASE statement creates a new CockroachDB database.
The CREATE DATABASE statement performs a schema change. For more information about how online schema changes work in CockroachDB, see Online Schema Changes.

Required privileges

To create a database, the user must be a member of the admin role or must have the CREATEDB parameter set.

Synopsis

create_database syntax diagram

Parameters

To control the maximum number of non-superuser (root user or other admin role) connections a gateway node can have open at one time, use the server.max_connections_per_gateway cluster setting. 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

Create fails (name already in use)

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

Create a multi-region database

Suppose you start a cluster with region and zone localities specified at startup. For this example, let’s use a demo cluster, with the --demo-locality flag to simulate a multi-region cluster:
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:

Create a multi-region database with a super region

To create a multi-region database and define a super region in the same statement, use the SUPER REGION clause:
The command returns:
Each super region member must be included in the database’s region list. The super region inherits the database survival goal until you assign it an explicit goal with ALTER DATABASE ... ALTER SUPER REGION ... SURVIVE. To inspect the super region and whether it has an explicit survival goal, use SHOW SUPER REGIONS:

Create a multi-region database with a secondary region

You can add a secondary region to a multi-region database for failover purposes. If the primary region 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.
  2. Issue a CREATE DATABASE statement like the following. It is the same as in the Create a multi-region database example, except that it adds a SECONDARY REGION {region} clause:
For more information about secondary regions, see Secondary regions. If your database contains REGIONAL BY ROW tables, do not set a secondary region. A database has only one secondary region, and it applies to every partition, so every partition fails over to the same region, however far away. Instead, use Zone Config Extensions to define a failover region for each database region. This works like a secondary region, but you choose the failover region separately for each partition’s home region, for example us-east1 → us-central1 and europe-west1 → europe-west2. Failover regions require SURVIVE REGION FAILURE. For instructions, see Configure per-partition failover regions for REGIONAL BY ROW tables.

See also