Warning:
Cockroach Labs will stop providing Assistance Support for this version on May 12, 2021. Prior to that date, upgrade to a more recent version to continue receiving support. For more details, see the Release Support Policy.

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

Note:
It is not possible to rename a database referenced by a view. For more details, see View Dependencies.

Synopsis

ALTER DATABASE name RENAME TO name

Required privileges

Only members of the admin role can rename databases. By default, the root user belongs to the admin role.

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.

Limitations

If an ALTER DATABASE ... RENAME statement is issued on a single gateway node and a successful result is returned, it is possible to observe the old database names in transactions on other gateway nodes for a short amount of time after the rename is executed. This issue is specific to databases, which have their metadata stored in an incoherent cache, unlike tables. Note that this issue is resolved in v20.2.

Examples

Rename a Database

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

Rename fails (new name already in use)

copy
icon/buttons/copy
> ALTER DATABASE db2 RENAME TO db3;
pq: the new database name "db3" already exists

See also



YesYes NoNo