ALTER SCHEMA statement modifies a user-defined schema. CockroachDB currently supports changing the name of the schema and the owner of the schema.
The
ALTER SCHEMA statement performs a schema change. For more information about how online schema changes work in CockroachDB, see Online Schema Changes.Syntax
Parameters
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 role. The new owner role must also have the
CREATEprivilege on the database to which the schema belongs.
Examples
Setup
To follow along, runcockroach demo to start a temporary, in-memory cluster with the movr sample dataset preloaded:
Rename a schema
You cannot rename a schema if a table in the schema is used by a view or user-defined function.
root, and create a new user max and a schema org_one with max as the owner:
ALTER SCHEMA command as a non-owner of the schema (i.e., root), CockroachDB returns an error.
Drop the schema and create it again, this time with root as the owner.
root, query the pg_catalog.pg_namespace and pg_catalog.pg_users tables:
Change the owner of a schema
Suppose that you access the SQL shell as userroot, and create a new schema named org_one:
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.
max, query the pg_catalog.pg_namespace and pg_catalog.pg_users tables:

