Create a Database

On this page Carat arrow pointing down
Warning:
CockroachDB v22.1 is no longer supported. For more details, see the Release Support Policy.

This page provides best-practice guidance on creating databases, with a couple examples based on Cockroach Labs' fictional vehicle-sharing company, MovR.

Tip:

For reference documentation on the CREATE DATABASE statement, including additional examples, see the CREATE DATABASE syntax page.

Before you begin

Before reading this page, do the following:

Create a database

Database objects make up the first level of the CockroachDB naming hierarchy.

To create a database, use a CREATE DATABASE statement, following the database best practices. After reviewing the best practices, see the examples we provide below.

Database best practices

Here are some best practices to follow when creating and using databases:

Example

Create an empty file with the .sql file extension at the end of the filename. This file will initialize the database that will store all of the data for the MovR application.

For example:

icon/buttons/copy
$ touch dbinit.sql

Open dbinit.sql in a text editor, and, at the top of the file, add a CREATE DATABASE statement:

icon/buttons/copy
CREATE DATABASE IF NOT EXISTS movr;

This statement will create a database named movr, if one does not already exist.

To execute the statement in the dbinit.sql file as the root user, run the following command:

icon/buttons/copy
$ cockroach sql \
--certs-dir={certs-directory} \
--user=root \
-f dbinit.sql

To view the database in the cluster, execute a SHOW DATABASES statement from the command line:

icon/buttons/copy
$ cockroach sql \
--certs-dir={certs-directory} \
--user=root \
--execute="SHOW DATABASES;"
  database_name | owner | primary_region | regions | survival_goal
----------------+-------+----------------+---------+----------------
  defaultdb     | root  | NULL           | {}      | NULL
  movr          | root  | NULL           | {}      | NULL
  postgres      | root  | NULL           | {}      | NULL
  system        | node  | NULL           | {}      | NULL
(4 rows)

You're now ready to start adding user-defined schemas to the movr database.

For guidance on creating user-defined schemas, see Create a User-defined Schema.

What's next?

You might also be interested in the following pages:


Yes No
On this page

Yes No