ALTER DATABASE ... RENAME TO

On this page Carat arrow pointing down
Warning:
CockroachDB v19.2 is no longer supported as of May 12, 2021. For more details, refer to 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

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

Rename fails (new name already in use)

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

See also


Yes No
On this page

Yes No