> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cockroachlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a Database

This page provides best-practice guidance on creating databases, with a couple examples based on Cockroach Labs's fictional vehicle-sharing company, [MovR](https://docs.cockroachlabs.com/docs/v24.1/movr.md).

> [!TIP]
>
> For reference documentation on the `CREATE DATABASE` statement, including additional examples, see the [`CREATE DATABASE` syntax page](https://docs.cockroachlabs.com/docs/v24.1/create-database).

## Before you begin

Before reading this page, do the following:

* [Create a CockroachDB Standard cluster](https://docs.cockroachlabs.com/docs/cockroachcloud/quickstart.md) or [start a local cluster](https://docs.cockroachlabs.com/docs/v24.1/start-a-local-cluster.md).
* [Review the database schema objects](https://docs.cockroachlabs.com/docs/v24.1/schema-design-overview.md).

## Create a database

Database objects make up the first level of the [CockroachDB naming hierarchy](https://docs.cockroachlabs.com/docs/v24.1/sql-name-resolution.md#naming-hierarchy).

To create a database, use a [`CREATE DATABASE` statement](https://docs.cockroachlabs.com/docs/v24.1/create-database), following [the database best practices](#database-best-practices). After reviewing the best practices, see the examples we provide [below](#example).

> [!NOTE]
>
> Cockroach Labs recommends against starting a database name with the string `cluster:`. Refer to [Database Best Practices](#database-best-practices) for more information.

### Database best practices

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

* Do not use the preloaded `defaultdb` database. Instead, create your own database with a `CREATE DATABASE` statement, and change it to the SQL session's [current database](https://docs.cockroachlabs.com/docs/v24.1/sql-name-resolution.md#current-database) by executing a `USE {databasename};` statement, by passing the `--database={databasename}` flag to the [`cockroach sql` command](https://docs.cockroachlabs.com/docs/v24.1/cockroach-sql#general), or by specifying the `database` parameter in the [connection string](https://docs.cockroachlabs.com/docs/v24.1/connection-parameters.md#connect-using-a-url) passed to your database schema migration tool.
* Do not begin 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 [connect to the cluster](https://docs.cockroachlabs.com/docs/v24.1/connect-to-the-database.md): `&options=-ccluster=system`
* Create databases and [user-defined schemas](https://docs.cockroachlabs.com/docs/v24.1/schema-design-schema) as a member of [the `admin` role](https://docs.cockroachlabs.com/docs/v24.1/security-reference/authorization#admin-role) (e.g., as the [`root` user](https://docs.cockroachlabs.com/docs/v24.1/security-reference/authorization)), and create all other lower-level objects as a [different user](https://docs.cockroachlabs.com/docs/v24.1/schema-design-overview.md#control-access-to-objects), with fewer privileges, following [authorization best practices](https://docs.cockroachlabs.com/docs/v24.1/security-reference/authorization.md#authorization-best-practices).
* Limit the number of databases you create. If you need to create multiple tables with the same name in your cluster, do so in different [user-defined schemas](https://docs.cockroachlabs.com/docs/v24.1/schema-design-schema), in the same database.
* Use a [database schema migration tool](https://docs.cockroachlabs.com/docs/v24.1/third-party-database-tools.md#schema-migration-tools) or the [CockroachDB SQL client](https://docs.cockroachlabs.com/docs/v24.1/cockroach-sql.md) instead of a [client library](https://docs.cockroachlabs.com/docs/v24.1/third-party-database-tools.md#drivers) to execute [database schema changes](https://docs.cockroachlabs.com/docs/v24.1/online-schema-changes.md).

### 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:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ touch dbinit.sql
```

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

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
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:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach sql \
--certs-dir={certs-directory} \
--user=root \
-f dbinit.sql
```

To view the database in the cluster, execute a [`SHOW DATABASES`](https://docs.cockroachlabs.com/docs/v24.1/show-databases) statement from the command line:

```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
$ cockroach sql \
--certs-dir={certs-directory} \
--user=root \
--execute="SHOW DATABASES;"
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
  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](https://docs.cockroachlabs.com/docs/v24.1/schema-design-schema).

## What's next?

* [Create a User-defined Schema](https://docs.cockroachlabs.com/docs/v24.1/schema-design-schema)
* [Create a Table](https://docs.cockroachlabs.com/docs/v24.1/schema-design-table)

You might also be interested in the following pages:

* [`CREATE DATABASE`](https://docs.cockroachlabs.com/docs/v24.1/create-database)
* [Schema Design Overview](https://docs.cockroachlabs.com/docs/v24.1/schema-design-overview.md)
* [`cockroach` Commands Overview](https://docs.cockroachlabs.com/docs/v24.1/cockroach-commands)
* [CockroachDB naming hierarchy](https://docs.cockroachlabs.com/docs/v24.1/sql-name-resolution.md#naming-hierarchy)
