PostgreSQLで作成したデータベース名を別名に変更する方法 †
PostgreSQLでデータベース名を変更したい場合の手順を以下に記します。
ALTER DATABASE †
ALTER DATABASE コマンドを使用することによりデータベース名を変更することができます。
データベース名の変更コマンドは以下の構文になります。
ALTER DATABASE 変更前DB名 RENAME TO 変更後DB名;
- PostgreSQLのデータベース一覧を表示しています。
以下に表示されている、sakuradb tsubakidbに変更してみます。
$ 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)
- 以下のコマンドを実行します。
尚、ユーザは postgres を使用します。
- ターミナルから直接実行する場合は -c オプションでコマンドを指定します。
以下、実際に実行したときの出力です。
tsubakidbになっているのが確認できます。
$ 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で接続し、実行する場合
psqlコマンドでPostgreSQLに接続する場合は、postgresに接続しコマンドを実行します。
下記は、PostgreSQLに接続、変更前データベース一覧の表示(\l)、データベース名変更コマンド実行(ALTER DATABASE ...)、変更後データベース一覧の表示(\l)と手順となっています。
$ 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でデータベース名を変更する手順でした。