PostgreSQLでデータベース名を変更したい場合の手順を以下に記します。
ALTER DATABASE コマンドを使用することによりデータベース名を変更することができます。
データベース名の変更コマンドは以下の構文になります。
ALTER DATABASE 変更前DB名 RENAME TO 変更後DB名;
$ psql -l -U postgres -h localhost
Password for user postgres:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+--------------------+--------------------+-----------------------
postgres | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
sakuradb | sakura | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
template0 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
$ psql -U postgres -h localhost -c "ALTER DATABASE sakuradb RENAME TO tsubakidb"
Password for user postgres:
ALTER DATABASE
$ psql -l -U postgres -h localhost
Password for user postgres:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+--------------------+--------------------+-----------------------
postgres | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
template0 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
tsubakidb | sakura | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
(4 rows)
$ psql -U postgres -h localhost postgres
Password for user postgres:
psql (9.6.0, server 9.2.17)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+--------------------+--------------------+-----------------------
postgres | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
sakuradb | sakura | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
template0 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres=# alter database sakuradb rename to tsubakidb;
ALTER DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+--------------------+--------------------+-----------------------
postgres | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
template0 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 | =c/postgres +
| | | | | postgres=CTc/postgres
tsubakidb | sakura | UTF8 | Japanese_Japan.932 | Japanese_Japan.932 |
(4 rows)
postgres=# \q
$
以上、PostgreSQLでデータベース名を変更する手順でした。