Table of Contents
MySQL の場合は MySQL: ダンプを保存せずにデータベースをコピー をご覧ください。
PostgreSQL のデータを移す際によくやるのは、 pg_dump でダンプデータを取得・保存して、 psql で流し込む方法です。
いちいちダンプファイルに保存するのも面倒なので、つなげてひとつのコマンドにしてみました。 ダンプファイルの保存は行いません。
Without Password
Usually, database is protected with password, but now let’s check the basic.
|
1 |
pg_dump db_name_1 -h host_1 -U user_name_1 | psql db_name_2 -h host_2 -U user_name_2 |
With Password
エクスポート元、インポート先のどちらかにパスワードがある場合
パスワードを聞かれた時に入力できるので、 必要に応じて -W をつければ OK です。
|
1 |
pg_dump db_name_1 -h host_1 -U user_name_1 -W | psql db_name_2 -h host_2 -U user_name_2 |
|
1 |
pg_dump db_name_1 -h host_1 -U user_name_1 | psql db_name_2 -h host_2 -U user_name_2 -W |
エクスポート元、インポート先の両方にパスワードがある場合
-W オプションをつけても、パスワードをうまく入力することができません(私はいい方法をみつけることができませんでした)。 そこで .pgpass ファイル を使います。
.pgpass
If you save password into .pgpass and locate it in your home directory, you can connect to the databases of PostgreSQL without password.
.pgpass には次のように記述します。
|
1 2 |
example.com:5432:db_name_1:user_name_1:password_1 111.111.111.111:5432:db_name_2:user_name_2:password_2 |
From the left most, host, IP address, port number, database name, user name and password, connected by “:”. If you have several database configurations, write them into multi lines.
And set the permission of the .pgpass 600, then .pgpass will be available.
Creating .pgpass, you can export and import in the same time. You don’t have to add -W.
|
1 |
pg_dump db_name_1 -h host_1 -U user_name_1 | psql db_name_2 -h host_2 -U user_name_2 |
