On this page
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
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
> SHOW DATABASES;
+---------------+
| database_name |
+---------------+
| db1 |
| db2 |
| defaultdb |
| postgres |
| system |
+---------------+
(5 rows)
> ALTER DATABASE db1 RENAME TO db3;
> SHOW DATABASES;
+---------------+
| database_name |
+---------------+
| db2 |
| db3 |
| defaultdb |
| postgres |
| system |
+---------------+
(5 rows)
Rename fails (new name already in use)
> ALTER DATABASE db2 RENAME TO db3;
pq: the new database name "db3" already exists