

# 移行元から移行先への PostgreSQL データベースの転送
<a name="PostgreSQL.TransportableDB.Transporting"></a>

[PostgreSQL データベース移行の設定](PostgreSQL.TransportableDB.Setup.md) で記載したプロセスを完了すると、移行をスタートすることが可能です。移行をスタートするには、移行先 DB インスタンスで `transport.import_from_server` 関数を実行します。次の構文では、関数に使用するパラメータを確認できます。

```
SELECT transport.import_from_server( 
   'source-db-instance-endpoint', 
    source-db-instance-port, 
   'source-db-instance-user', 
   'source-user-password', 
   'source-database-name', 
   'destination-user-password', 
   false);
```

この例での `false` 値は、この処理がドライランではないことを関数に伝えます。移行の設定をテストするには、以下のように、関数の呼び出し時に `dry_run` オプションで `true` を指定します。

```
postgres=> SELECT transport.import_from_server(
    'docs-lab-source-db.666666666666aws-region.rds.amazonaws.com', 5432,
    'postgres', '********', 'labdb', '******', true);
INFO:  Starting dry-run of import of database "labdb".
INFO:  Created connections to remote database        (took 0.03 seconds).
INFO:  Checked remote cluster compatibility          (took 0.05 seconds).
INFO:  Dry-run complete                         (took 0.08 seconds total).
 import_from_server
--------------------

(1 row)
```

`pg_transport.timing` パラメータがデフォルト値の `true` に設定されているため、INFO 行が出力されます。次に示すように、`dry_run` に `false` を設定してコマンドを実行し、データベースを移行元から移行先にインポートします。

```
INFO:  Starting import of database "labdb".
INFO:  Created connections to remote database        (took 0.02 seconds).
INFO:  Marked remote database as read only           (took 0.13 seconds).
INFO:  Checked remote cluster compatibility          (took 0.03 seconds).
INFO:  Signaled creation of PITR blackout window     (took 2.01 seconds).
INFO:  Applied remote database schema pre-data       (took 0.50 seconds).
INFO:  Created connections to local cluster          (took 0.01 seconds).
INFO:  Locked down destination database              (took 0.00 seconds).
INFO:  Completed transfer of database files          (took 0.24 seconds).
INFO:  Completed clean up                            (took 1.02 seconds).
INFO:  Physical transport complete              (took 3.97 seconds total).
import_from_server
--------------------
(1 row)
```

この関数には、データベースユーザーパスワードを入力する必要があります。よって、移行完了後は、使用したユーザーロールのパスワードを変更することをお勧めします。または、SQL のバインド可変を使用するとユーザーロールを一時的に作成することができます。このようなテンポラリロールを移行に使ったら、破棄することが可能です。

移行が成功しなかった場合、次のようなエラーメッセージが表示されることがあります。

```
pg_transport.num_workers=8 25% of files transported failed to download file data
```

「failed to download file data (ファイルデータのダウンロードに失敗しました)」というエラーメッセージは、データベースのサイズに対してワーカープロセスの数が正しく設定されていないことを示します。`pg_transport.num_workers` に設定した値の増減が必要な場合があります。失敗が発生するたびに、処理の完了率がレポートされるため、変更の影響度合いを確認できます。例えば、あるケースで設定を 8 から 4 に変更した場合、次のような結果になります。

```
pg_transport.num_workers=4 75% of files transported failed to download file data
```

`max_worker_processes` パラメーターは、移行のプロセス中にも考慮されることにご留意ください。つまり、データベースを正常に移行するためには、`pg_transport.num_workers` と `max_worker_processes` の両方で変更が必要な場合があります。`pg_transport.num_workers` に 2 を設定することで、最終的にこの例は正しく機能します。

```
pg_transport.num_workers=2 100% of files transported
```

`transport.import_from_server` 関数とそのパラメータの詳細については、「[トランスポータブルデータベースの関数リファレンス](PostgreSQL.TransportableDB.transport.import_from_server.md)」を参照してください。