CREATE DATABASE

On this page Carat arrow pointing down

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 create new databases. By default, the root user belongs to the admin role.

Synopsis

CREATE DATABASE IF NOT EXISTS name WITH ENCODING = encoding

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 identifier rules.
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'.

Example

Create a database

icon/buttons/copy
> CREATE DATABASE bank;
icon/buttons/copy
> SHOW DATABASES;
+---------------+
| database_name |
+---------------+
| bank          |
| defaultdb     |
| postgres      |
| system        |
+---------------+
(4 rows)

Create fails (name already in use)

icon/buttons/copy
> CREATE DATABASE bank;
pq: database "bank" already exists
icon/buttons/copy
> 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.

icon/buttons/copy
> SHOW DATABASES;
+---------------+
| database_name |
+---------------+
| bank          |
| defaultdb     |
| postgres      |
| system        |
+---------------+
(4 rows)

See also


Yes No
On this page

Yes No