ALTER DATABASE ... RENAME TO

The RENAME TO clause is part of ALTER DATABASE, and changes the name of a database.

Note:

This statement performs a schema change. For more information about how online schema changes work in CockroachDB, see Online Schema Changes.

Synopsis

ALTER DATABASE database_name RENAME TO database_name

Required privileges

To rename a database, the user must be a member of the admin role or must have the CREATEDB parameter set.

Parameters

Parameter Description
name The first instance of name is the current name of the database. The second instance is the new name for the database. The new name must be unique and follow these identifier rules. You cannot rename a database if it is set as the current database or if sql_safe_updates = true.

Viewing schema changes

This schema change statement is registered as a job. You can view long-running jobs with SHOW JOBS.

Limitations

  • It is not possible to rename a database if the database is referenced by a view. For more details, see View Dependencies.

Examples

Rename a database

icon/buttons/copy
> CREATE DATABASE db1;
icon/buttons/copy
> SHOW DATABASES;
  database_name
-----------------
  db1
  defaultdb
  movr
  postgres
  system
(5 rows)
icon/buttons/copy
> ALTER DATABASE db1 RENAME TO db2;
icon/buttons/copy
> SHOW DATABASES;
  database_name
-----------------
  db2
  defaultdb
  movr
  postgres
  system
(5 rows)

Rename fails (new name already in use)

icon/buttons/copy
> ALTER DATABASE db2 RENAME TO movr;
ERROR: the new database name "movr" already exists
SQLSTATE: 42P04

See also

YesYes NoNo