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

# Create a Database

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

This page provides best-practice guidance on creating databases, with a couple examples based on Cockroach Labs's fictional vehicle-sharing company, <InternalLink path="movr">MovR</InternalLink>.

<Tip>
  For reference documentation on the `CREATE DATABASE` statement, including additional examples, see the <InternalLink path="create-database">`CREATE DATABASE` syntax page</InternalLink>.
</Tip>

## Before you begin

Before reading this page, do the following:

* <InternalLink version="cockroachcloud" path="quickstart">Create a CockroachDB Standard cluster</InternalLink> or <InternalLink path="start-a-local-cluster">start a local cluster</InternalLink>.
* <InternalLink path="schema-design-overview">Review the database schema objects</InternalLink>.

## Create a database

Database objects make up the first level of the <InternalLink path="sql-name-resolution#naming-hierarchy">CockroachDB naming hierarchy</InternalLink>.

To create a database, use a <InternalLink path="create-database">`CREATE DATABASE` statement</InternalLink>, 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.
</Note>

### 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 <InternalLink path="sql-name-resolution#current-database">current database</InternalLink> by executing a `USE {databasename};` statement, by passing the `--database={databasename}` flag to the <InternalLink path="cockroach-sql#general">`cockroach sql` command</InternalLink>, or by specifying the `database` parameter in the <InternalLink path="connection-parameters#connect-using-a-url">connection string</InternalLink> 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 <InternalLink path="connect-to-the-database">connect to the cluster</InternalLink>: `&options=-ccluster=system`
* Create databases and <InternalLink path="schema-design-schema">user-defined schemas</InternalLink> as a member of <InternalLink path="security-reference/authorization#admin-role">the `admin` role</InternalLink> (e.g., as the <InternalLink path="security-reference/authorization">`root` user</InternalLink>), and create all other lower-level objects as a <InternalLink path="schema-design-overview#control-access-to-objects">different user</InternalLink>, with fewer privileges, following <InternalLink path="security-reference/authorization#authorization-best-practices">authorization best practices</InternalLink>.
* 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 <InternalLink path="schema-design-schema">user-defined schemas</InternalLink>, in the same database.
* Use a <InternalLink path="third-party-database-tools#schema-migration-tools">database schema migration tool</InternalLink> or the <InternalLink path="cockroach-sql">CockroachDB SQL client</InternalLink> instead of a <InternalLink path="third-party-database-tools#drivers">client library</InternalLink> to execute <InternalLink path="online-schema-changes">database schema changes</InternalLink>.

### 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 <InternalLink path="show-databases">`SHOW DATABASES`</InternalLink> 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 <InternalLink path="schema-design-schema">Create a User-defined Schema</InternalLink>.

## What's next?

* <InternalLink path="schema-design-schema">Create a User-defined Schema</InternalLink>
* <InternalLink path="schema-design-table">Create a Table</InternalLink>

You might also be interested in the following pages:

* <InternalLink path="create-database">`CREATE DATABASE`</InternalLink>
* <InternalLink path="schema-design-overview">Schema Design Overview</InternalLink>
* <InternalLink path="cockroach-commands">`cockroach` Commands Overview</InternalLink>
* <InternalLink path="sql-name-resolution#naming-hierarchy">CockroachDB naming hierarchy</InternalLink>
