Skip to main content
The 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 admin role can create new schemas. By default, the root user belongs to the admin role.
  • To on a user-defined schema, a user must have the GRANT privilege 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 USAGE privilege on the schema.

Syntax

create_schema syntax diagram

Parameters

Example

Setup

To follow along, run to start a temporary, in-memory cluster with the sample dataset preloaded:

Create a schema

By default, the user executing the 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

SQL does not generate an error, even though a new schema wasn’t created.

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 an AUTHORIZATION clause to the CREATE SCHEMA statement:
If no schema name is specified in a CREATE SCHEMA statement with an AUTHORIZATION clause, the schema will be named after the user specified:
When you , CockroachDB looks for the table in the $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:
Then, 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:
Because max is the current user, all unqualified accounts table names resolve as max.accounts, and not public.accounts.

See also