Skip to main content
The ALTER SCHEMA modifies a user-defined . CockroachDB currently supports changing the name of the schema and the owner of the schema.
The “ statement performs a schema change. For more information about how online schema changes work in CockroachDB, see .

Syntax

alter_schema syntax diagram

Parameters

ParameterDescription
name
name.name
The name of the schema to alter, or the name of the database containing the schema and the schema name, separated by a “.”.
schema_new_nameThe name of the new schema. The new schema name must be unique within the database and follow these .
role_specThe role to set as the owner of the schema.

Required privileges

  • To rename a schema, the user must be the owner of the schema.
  • To change the owner of a schema, the user must be the current owner of the schema and a member of the new owner . The new owner role must also have the CREATE on the database to which the schema belongs.

Examples

Setup

The following examples use MovR, a fictional vehicle-sharing application, to demonstrate CockroachDB SQL statements. For more information about the MovR example application and dataset, see . To follow along, run to start a temporary, in-memory cluster with the movr dataset preloaded:

Rename a schema

You cannot rename a schema if a in the schema is used by a or .
Suppose that you access the as user root, and max and org_one with max as the owner:
Now, suppose you want to rename the schema:
Because you are executing the ALTER SCHEMA command as a non-owner of the schema (i.e., root), CockroachDB returns an error. and create it again, this time with root as the owner.
To verify that the owner is now root, query the pg_catalog.pg_namespace and pg_catalog.pg_users tables:
As its owner, you can rename the schema:

Change the owner of a schema

Suppose that you access the as user root, and named org_one:
Now, suppose that you want to change the owner of the schema org_one to an existing user named max. To change the owner of a schema, the current owner must belong to the role of the new owner (in this case, max), and the new owner must have CREATE privileges on the database.
To verify that the owner is now max, query the pg_catalog.pg_namespace and pg_catalog.pg_users tables:

See also