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 aCREATE DATABASE statement, following the database best practices. After reviewing the best practices, see the examples we provide below.
Cockroach Labs recommends against starting a database name with the string
cluster:. Refer to 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
defaultdbdatabase. Instead, create your own database with aCREATE DATABASEstatement, and change it to the SQL session’s current database by executing aUSE {databasename};statement, by passing the--database={databasename}flag to thecockroach sqlcommand, or by specifying thedatabaseparameter in the connection string 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:&options=-ccluster=system -
Create databases and user-defined schemas as a member of the
adminrole (e.g., as therootuser), and create all other lower-level objects as a different user, with fewer privileges, following 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, in the same database.
- Use a database schema migration tool or the CockroachDB SQL client instead of a client library to execute database schema changes.
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:
dbinit.sql in a text editor, and, at the top of the file, add a CREATE DATABASE statement:
movr, if one does not already exist.
To execute the statement in the dbinit.sql file as the root user, run the following command:
SHOW DATABASES statement from the command line:
movr database.
For guidance on creating user-defined schemas, see Create a User-defined Schema.

