On this page
Warning:
As of November 10, 2018, CockroachDB v1.0 is no longer supported. For more details, refer to the Release Support Policy.
The CREATE DATABASE
statement creates a new CockroachDB database.
Required Privileges
Only the root
user can create databases.
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 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
> CREATE DATABASE bank;
> SHOW DATABASES;
+----------+
| Database |
+----------+
| bank |
| system |
+----------+
Create Fails (Name Already In Use)
> SHOW DATABASES;
+----------+
| Database |
+----------+
| bank |
| system |
+----------+
> CREATE DATABASE bank;
pq: database "bank" already exists
> SHOW DATABASES;
+----------+
| Database |
+----------+
| bank |
| system |
+----------+
> 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.
> SHOW DATABASES;
+----------+
| Database |
+----------+
| bank |
| system |
+----------+