CREATE DATABASE

The CREATE DATABASE statement creates a new CockroachDB database.

Note:

This statement performs a schema change. For more information about how online schema changes work in CockroachDB, see Online Schema Changes.

Required privileges

Only members of the admin role can configure replication zones. By default, the root user belongs to the admin role.

Synopsis

CREATEDATABASEIFNOTEXISTSnameWITHENCODING=encoding

Parameters

ParameterDescription
IF NOT EXISTSCreate a new database only if a database of the same name does not already exist; if one does exist, do not return an error.
nameThe name of the database to create, which must be unique and follow these identifier rules.
encodingThe 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'.

Example

Create a database

  1. > CREATE DATABASE bank;
  1. > SHOW DATABASES;
  1. +---------------+
  2. | database_name |
  3. +---------------+
  4. | bank |
  5. | defaultdb |
  6. | postgres |
  7. | system |
  8. +---------------+
  9. (4 rows)

Create fails (name already in use)

  1. > CREATE DATABASE bank;
  1. pq: database "bank" already exists
  1. > CREATE DATABASE IF NOT EXISTS bank;

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

  1. > SHOW DATABASES;
  1. +---------------+
  2. | database_name |
  3. +---------------+
  4. | bank |
  5. | defaultdb |
  6. | postgres |
  7. | system |
  8. +---------------+
  9. (4 rows)

See also

Was this page helpful?
YesNo