The RENAME DATABASE statement 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.

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



Yes No