CREATE DATABASE creates a new CockroachDB database.
The “ statement performs a schema change. For more information about how online schema changes work in CockroachDB, see .
Required privileges
To create a database, the user must be a member of theadmin role or must have the parameter set.
Synopsis
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 and follow these . 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 : &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.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. (*) |
PRIMARY REGION region_name | Create a with region_name as .Allowed values include any region returned by . |
REGIONS region_name_list | Create a with region_name_list as .Allowed values include any region returned by . To set database regions at database creation, a primary region must be specified in the same CREATE DATABASE statement. |
SURVIVE ZONE FAILURE (Default)SURVIVE REGION FAILURE | Create a with regional failure or zone failure . To set the regional failure survival goal, the database must have at least 3 . Surviving zone failures is the default setting for multi-region databases. |
server.max_connections_per_gateway . 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)
CREATE DATABASE even though a new database wasn’t created.
Create a multi-region database
Suppose you start a cluster with region and zone . For this example, let’s use a , with the to simulate a multi-region cluster:Create a multi-region database with a secondary region
You can add a to a for failover purposes. If the fails, the secondary region becomes the new primary region. To add a secondary region during database creation, use the following steps:-
Start a
cockroach democluster as described in the example Create a multi-region database. -
Issue a
CREATE DATABASEstatement like the following. It is the same as in the Create a multi-region database example, except that it adds aSECONDARY REGION {region}clause:
See also
- .

