1、必须是数据库的属主才可以修改
2、数据库必须断开所有连接
highgo=> \c highgo test
You are now connected to database "highgo" as user "test".
highgo=> alter database test rename to testa;
错误: 必须是数据库 test 的属主
highgo=>
highgo=>
highgo=> \c highgo highgo
You are now connected to database "highgo" as user "highgo".
highgo=# alter database test rename to testa;
错误: 其他用户正在使用数据库 "test"
DETAIL: 那里有1个其它会话正在使用数据库.
highgo=# alter database test rename to testa;
ALTER DATABASE
highgo=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------+----------+------------+------------+-------------------
a | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
b | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
c | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
highgo | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
template0 | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/highgo +
| | | | | highgo=CTc/highgo
template1 | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/highgo +
| | | | | highgo=CTc/highgo
testa | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
(7 rows)
highgo=# alter database testa rename to test;
ALTER DATABASE
highgo=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------+----------+------------+------------+-------------------
a | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
b | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
c | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
highgo | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
template0 | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/highgo +
| | | | | highgo=CTc/highgo
template1 | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/highgo +
| | | | | highgo=CTc/highgo
test | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
(7 rows)
2、数据库必须断开所有连接
highgo=> \c highgo test
You are now connected to database "highgo" as user "test".
highgo=> alter database test rename to testa;
错误: 必须是数据库 test 的属主
highgo=>
highgo=>
highgo=> \c highgo highgo
You are now connected to database "highgo" as user "highgo".
highgo=# alter database test rename to testa;
错误: 其他用户正在使用数据库 "test"
DETAIL: 那里有1个其它会话正在使用数据库.
highgo=# alter database test rename to testa;
ALTER DATABASE
highgo=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------+----------+------------+------------+-------------------
a | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
b | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
c | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
highgo | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
template0 | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/highgo +
| | | | | highgo=CTc/highgo
template1 | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/highgo +
| | | | | highgo=CTc/highgo
testa | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
(7 rows)
highgo=# alter database testa rename to test;
ALTER DATABASE
highgo=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------+----------+------------+------------+-------------------
a | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
b | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
c | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
highgo | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
template0 | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/highgo +
| | | | | highgo=CTc/highgo
template1 | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/highgo +
| | | | | highgo=CTc/highgo
test | highgo | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
(7 rows)