CREATE SCHEMA creates a user-defined .
The CREATE SCHEMA statement performs a schema change. For more information about how online schema changes work in CockroachDB, see .
Required privileges
- Only members of the
adminrole can create new schemas. By default, therootuser belongs to theadminrole. - To on a user-defined schema, a user must have the
GRANTprivilege on the schema and the privilege that they want to grant. - To create or interact with objects that depend on a user-defined schema, a user must have the
USAGEprivilege on the schema.
Syntax
Parameters
| Parameter | Description |
|---|---|
IF NOT EXISTS | Create a new schema only if a schema of the same name does not already exist within the database. If one does exist, do not return an error. |
name name.name | The name of the schema to create, or the name of the database in which to create the schema and the schema name, separated by a “.”. The schema name must be unique within its database and follow these . |
AUTHORIZATION role\_spec | Optionally identify a user (role\_spec) to be the owner of the schema. If a CREATE SCHEMA statement has an AUTHORIZATION clause, but no schema name is specified, the schema will be named after the specified owner of the schema. If a CREATE SCHEMA statement does not have an AUTHORIZATION clause, the user executing the statement will be named the owner. |
Example
Setup
To follow along, run to start a temporary, in-memory cluster with the sample dataset preloaded:Create a schema
CREATE SCHEMA statement is the owner of the schema. For example, suppose you created the schema as user root. root would be the owner of the schema.
Create a schema if one does not exist
Create two tables of the same name in different schemas
You can create tables of the same name in the same database if they are in separate schemas.Create a schema with authorization
To specify the owner of a schema, add anAUTHORIZATION clause to the CREATE SCHEMA statement:
CREATE SCHEMA statement with an AUTHORIZATION clause, the schema will be named after the user specified:
$user schema (i.e., a schema named after the current user). If no schema exists with the name of the current user, the public schema is used.
For example, suppose that you (i.e., the role of the current user root) to the max user:
max and creates two tables of the same name, in the same database, one in the max schema, and one in the public schema:
max then inserts some values into the accounts table, without specifying a schema:
max is the current user, all unqualified accounts table names resolve as max.accounts, and not public.accounts.

