> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-parallel-read-in-order-multi-part.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ClickPipes のデータソースを使用した PostgreSQL データの移行

> ClickPipes を使用して、PostgreSQL データベースを ClickHouse Managed Postgres に移行する方法を学びます。

export const Image = ({img, alt, size = "lg", background}) => {
  const normalizedSize = ["sm", "md", "lg"].includes(size) ? size : "lg";
  const backgroundColor = background === "white" ? "white" : background === "black" ? "rgb(31 31 28)" : undefined;
  return <div className={`ch-image-${normalizedSize}`}>
      <Frame>
        <img src={img} alt={alt} style={{
    backgroundColor
  }} />
      </Frame>
    </div>;
};

export const BetaBadge = ({link, galaxyTrack, galaxyEvent}) => {
  if (link) {
    return <a href={link} target="_blank" rel="noopener noreferrer" className="betaBadge" onClick={galaxyTrack && galaxyEvent ? galaxyOnClick(galaxyEvent) : undefined}>
                <span>ベータ</span>
            </a>;
  }
  return <a href="https://clickhouse.com/docs/reference/settings/beta-and-experimental-features#beta-features" className="betaBadge">
            <span>ベータ機能</span>
        </a>;
};

<BetaBadge link="https://clickhouse.com/cloud/postgres" galaxyTrack={true} galaxyEvent="docs.managed-postgres.migration-guide-clickhouse-cloud-beta" />

ClickHouse Cloud では、外部の PostgreSQL データベースを Managed Postgres サービスに移行するための ClickPipes をご利用いただけるようになりました。この組み込みインテグレーションにより、ソースデータベースへの接続、スキーマのエクスポート、Managed Postgres へのインポート、継続的なレプリケーションの設定をスムーズに行えます。

<div id="prerequisites">
  ## 前提条件
</div>

* レプリケーション権限を持つユーザーで、ソース PostgreSQL データベースにアクセスできること。ソースに応じて、以下のセットアップガイドに従ってください。
  * [Amazon RDS Postgres](/ja/integrations/clickpipes/postgres/source/rds)
  * [Amazon Aurora Postgres](/ja/integrations/clickpipes/postgres/source/aurora)
  * [Supabase Postgres](/ja/integrations/clickpipes/postgres/source/supabase)
  * [Google Cloud SQL Postgres](/ja/integrations/clickpipes/postgres/source/google-cloudsql)
  * [Azure Flexible Server for Postgres](/ja/integrations/clickpipes/postgres/source/azure-flexible-server-postgres)
  * [Neon Postgres](/ja/integrations/clickpipes/postgres/source/neon-postgres)
  * [Crunchy Bridge Postgres](/ja/integrations/clickpipes/postgres/source/crunchy-postgres)
  * [TimescaleDB](/ja/integrations/clickpipes/postgres/source/timescale)
  * その他のプロバイダーまたはセルフホストのインスタンスについては、[Generic Postgres Source](/ja/integrations/clickpipes/postgres/source/generic)
* 移行先として ClickHouse Managed Postgres サービスが必要です。まだ用意していない場合は、[クイックスタート](/ja/products/managed-postgres/quickstart) を参照してください。
* ローカルマシンに `pg_dump` と `psql` がインストールされていること。どちらも標準の PostgreSQL クライアントツールに含まれています。

<div id="considerations">
  ## 移行前の注意事項
</div>

* **DDL の伝播**: 継続的レプリケーション (CDC) は、DML 操作と `ADD COLUMN` を取り込みます。`DROP COLUMN` や `ALTER COLUMN` など、その他の DDL 変更は伝播されないため、ターゲット側で手動で適用する必要があります。

<Note>
  移行中に問題が発生した場合は、よくあるエラーとその解決策について [Managed Postgres 移行のよくある質問](/ja/products/managed-postgres/migrations/faq) を確認してください。
</Note>

<h2 id="step-1-connect">
  Step 1: ソースデータベースに接続する
</h2>

[ClickHouse Cloud console](https://clickhouse.cloud) を開き、Managed Postgres サービスを選択します。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/servicecard.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=5da06beedac095dc527e7a83460f863e" alt="ClickHouse Cloud のサービス一覧に表示される Managed Postgres のサービスカード" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/servicecard.webp" />

左側のサイドバーで、**データソース** をクリックします。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/overview.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=0fffed584827f3fb1386d27fc205b81a" alt="Managed Postgres サービスのサイドバーにある データソース エントリ" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/overview.webp" />

**Start import** をクリックします。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/startimport.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=4b335dc3b56a6bfa1a9ca04dc9447e56" alt="Start import ボタンが表示された データソース ページ" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/startimport.webp" />

ソース PostgreSQL データベースの接続情報 (ホスト、ポート、ユーザー名、パスワード、データベース名) を入力します。ソース側で必要な場合は、**TLS** を有効にします。

ソースデータベースへのプライベート接続が必要な場合は、**SSH トンネリング** を選択し、必要な SSH 情報を入力できます。これにより、公開されていないデータベースにも移行処理から安全に接続できます。

インジェスト方法を選択します。

* **初期ロード + CDC** — 既存データをコピーした後、継続的な変更を反映してターゲットを同期し続けます。
* **初期ロード only** — 一回限りのコピーで、継続的なレプリケーションは行いません。
* **CDC only** — 初期コピーをスキップし、この時点以降の新しい変更だけをレプリケートします。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/migrationform.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=f360daf02b6b04240a0d9e9e7f143498" alt="ステップ 1: インジェスト方法のオプションを含むソースデータベース接続フォーム" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/migrationform.webp" />

**Next** をクリックします。

<h2 id="automated-schema-migration">
  自動スキーマ移行
</h2>

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pg_dump_restore/automated.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=9951eb0470ed0a63c40543b2285057b9" alt="ステップ 2: 宛先データベースセレクターを使用した自動スキーマ移行" size="lg" border width="2632" height="742" data-path="images/managed-postgres/pg_dump_restore/automated.webp" />

このオプションを選択すると、ClickPipe の作成後、[Setup フェーズ](/ja/integrations/clickpipes/postgres/lifecycle#setup)中にソースデータベースのスキーマが自動的に取得され、Managed Postgres サービスに適用されます。

この機能では、後でウィザードで選択するテーブルにかかわらずソースデータベース内のすべてのデータベースオブジェクトを取得するため、**空の宛先データベース**が前提となります。宛先データベースに既存のデータがある場合や、より細かく設定したい場合は、代わりに[**手動**](#manual-schema-migration)モードを選択する必要があります。

ドロップダウンから宛先データベースを選択するか、**新しいデータベースを作成**をクリックして作成します。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pg_dump_restore/newdb.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=7af9cb67d83ba390fa0dfbe9a26da87d" alt="新しい Postgres データベースを作成するダイアログ" size="lg" border width="1642" height="742" data-path="images/managed-postgres/pg_dump_restore/newdb.webp" />

<h3 id="monitoring">
  監視
</h3>

ClickPipes の詳細ビューで、スキーマ移行の進行状況を確認できます。**ログ**にはスキーマ移行のステータスが表示され、発生したエラーも表示されます。

このモードには、次の制限があります。

* SSH トンネリングを使用するパイプでは、自動スキーマ移行を使用できません。スキーマは[手動でエクスポートおよびインポート](#manual-schema-migration)する必要があります。

<h2 id="manual-schema-migration">
  手動スキーマ移行
</h2>

移行先データベースにすでにデータが存在する場合や、自動モードで想定されるクリーンな状態ではなく、よりカスタマイズしたセットアップを行う場合は、ここで**手動**モードを選択できます。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pg_dump_restore/manual.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=bcb9482e3070bd505e42dd20b49949e5" alt="ステップ2: pg_dumpエクスポートコマンドによる手動スキーマ移行" size="lg" border width="2632" height="1022" data-path="images/managed-postgres/pg_dump_restore/manual.webp" />

<h3 id="step-2-export-schema">
  データベーススキーマをエクスポートする
</h3>

ウィザードには、ソースへの接続情報があらかじめ入力された `pg_dump` コマンドが表示されます。これをターミナルで実行します。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/nextexport.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=817489659403b6183ea9ca726b4c1c85" alt="ステップ 2: スキーマのエクスポート用 `pg_dump` コマンド" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/nextexport.webp" />

```shell theme={null}
pg_dump \
  -h <source_host> \
  -U <source_user> \
  -d <source_database> \
  --schema-only \
  -f pg.sql
```

これにより、`pg.sql` が現在のディレクトリに作成されます。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/psqlexport.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=8cb54def65d2d7d581686c4339aa0df2" alt="`pg_dump` 実行後のターミナル出力" size="lg" border width="1452" height="422" data-path="images/managed-postgres/pgpg/psqlexport.webp" />

**Next** をクリックします。

<h3 id="step-3-import-schema">
  Managed Postgres サービスにスキーマをインポートする
</h3>

ドロップダウンから宛先データベースを選択するか、**新しいデータベースを作成** をクリックして新しく作成します。

ウィザードには、スキーマダンプを Managed Postgres サービスに適用するための `psql` コマンドが表示されます。これをターミナルで実行します。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/nextimport.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=59f3694d70eca58c60c9cb2f64509cd0" alt="ステップ 3: スキーマのインポート用 `psql` コマンド" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/nextimport.webp" />

```shell theme={null}
psql \
  -h <target_host> \
  -p 5432 \
  -U <target_user> \
  -d <target_database> \
  -f pg.sql
```

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/psqlimport.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=3b4d6f4d7502e549ff3db7aa2e7b9c4a" alt="`psql` によるスキーマのインポート実行後のターミナル出力" size="lg" border width="2362" height="762" data-path="images/managed-postgres/pgpg/psqlimport.webp" />

**Next** をクリックします。

<h2 id="step-4-ingestion-settings">
  Step 4: インジェスト設定を構成する
</h2>

論理レプリケーションに使用するパブリケーションを指定します。空欄のままにすると、パブリケーションは自動的に作成されます。

スループットを調整するには、**高度なレプリケーション設定**を展開します。

| 設定 | デフォルト | 説明 |
| - | - | - |
| 同期間隔 (秒) | 10 | レプリケーションスロットをポーリングする間隔 |
| 初期ロードの並列スレッド数 | 4 | bulk copy フェーズで使用するスレッド数 |
| Pull バッチサイズ | 100,000 | レプリケーションの各バッチで取得する行数 |
| スナップショットのパーティションあたりの行数 | 100000 | 大規模なテーブルスナップショットのパーティションサイズ |
| 並列でスナップショットを作成するテーブル数 | 1 | 同時実行でスナップショットを作成するテーブル数 |

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/advancedsettings.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=b6c4dcaefdf8176f3d61636fabe70b95" alt="ステップ 4: パブリケーションと高度なレプリケーションオプションを含むインジェスト設定フォーム" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/advancedsettings.webp" />

**Next** をクリックします。

<div id="step-5-select-tables">
  ## ステップ5: テーブルを選択
</div>

レプリケートするテーブルを選択します。テーブルはスキーマごとにグループ化されています。個別にテーブルを選択するか、スキーマを展開してその中のすべてのテーブルを選択します。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/tablepicker.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=275772f47c4cbb55fdac96acdf8a8735" alt="ステップ5: スキーマごとにグループ化されたテーブル選択画面。［移行を作成］ボタン付き" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/tablepicker.webp" />

**移行を作成** をクリックします。

<h2 id="monitor">
  移行を監視する
</h2>

移行を作成すると、**データソース**にステータスが **実行中** として表示されます。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/migrationlist.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=a8e8712ecd01d1bf7b6112ba26275965" alt="実行中の移行が表示されたデータソース一覧" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/migrationlist.webp" />

移行をクリックすると、詳細ビューが開きます。**テーブル** タブには、処理済み行数、パーティション数、パーティションあたりの平均時間など、各テーブルの初期ロードの進行状況が表示されます。**メトリクス** タブには、CDC が開始されるとレプリケーションラグとスループットが表示されます。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/4n4lv5U169Tcsz6l/images/managed-postgres/pgpg/initialload.webp?fit=max&auto=format&n=4n4lv5U169Tcsz6l&q=85&s=8efa910d3f00341a552b81aca49b9c05" alt="テーブルごとの初期ロード統計を表示する移行詳細ビュー" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/initialload.webp" />

<h2 id="cutover">
  トラフィックの切り替え
</h2>

初期ロードが完了し、CDC (変更データキャプチャ) を使用している場合はレプリケーションラグがほぼゼロになった時点で、ソースの Postgres データベースから Managed Postgres サービスへトラフィックを切り替えられます。移行の詳細ビューを開き、**移行後の手順** タブを選択してください。このガイド付きウィザードでは、移行を安全に完了するために必要な 6 つのステップを順に案内します。次のステップへ進むには、その前のステップを完了しておく必要があります。

<h3 id="cutover-read-only">
  ステップ1: ソース Postgres データベースを読み取り専用モードに設定する
</h3>

カットオーバー中に差異が生じないよう、ソース側でアプリケーションからの書き込みを停止します。ウィザードには、ソースデータベース名があらかじめ入力された `ALTER DATABASE` コマンドが表示されるので、これをソースデータベース上で実行してください。

```sql theme={null}
ALTER DATABASE "<source_db>" SET default_transaction_read_only = on;
```

次に、既存の接続をすべて切断し、read-only 設定が反映され、転送中の書き込みが残らない状態にします。

```sql theme={null}
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = '<source_db>'
  AND pid <> pg_backend_pid();
```

<Note>
  Postgres の provider によっては、この手順が異なる場合があります。managed service では `ALTER DATABASE` や backend の強制終了が制限されていることがあり、代わりに独自の Console、パラメータグループ、あるいは書き込み privileges の剥奪によって読み取り専用モードを提供している場合があります。ソースを読み取り専用にする同等の方法については、ご利用の provider のドキュメントを参照してください。
</Note>

続行するには **Mark as completed** をクリックします。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/y9c9rzJ7kvnfRKJa/images/managed-postgres/pgpg/post-migration/readonly.webp?fit=max&auto=format&n=y9c9rzJ7kvnfRKJa&q=85&s=b5e1558c444dbf18d4a85850a9e2fe4d" alt="移行後の手順 1: ソースデータベースを読み取り専用モードに設定する" size="lg" border width="1486" height="1818" data-path="images/managed-postgres/pgpg/post-migration/readonly.webp" />

<h3 id="cutover-validate">
  ステップ2: 行数を検証する
</h3>

検証対象とするレプリケートテーブルを選択します。ClickPipes は選択された各テーブルの行数をソースとターゲットの両方でカウントし、比較します。大規模なテーブルでは、概算の行数が返される場合があります。すべてのテーブルを検証する場合は **Select all tables** を使用し、個別に指定する場合は対象のテーブルを検索してトグルで選択したうえで、**Count rows** をクリックします。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/GFspt4gULfSkoH59/images/managed-postgres/pgpg/post-migration/validate-row-counts.webp?fit=max&auto=format&n=GFspt4gULfSkoH59&q=85&s=2a376bdc0028485f26c4e3f159b7fec4" alt="移行後の手順 2: select tables and validate row counts between source and target" size="lg" border width="1452" height="1612" data-path="images/managed-postgres/pgpg/post-migration/validate-row-counts.webp" />

<h3 id="cutover-pause">
  ステップ3: パイプを一時停止する
</h3>

シーケンスをリセットしてトラフィックを切り替える前にレプリケーションを停止するため、ClickPipe を一時停止します。**Pause ClickPipe** をクリックし、パイプが一時停止するまで待ってから次に進んでください。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/y9c9rzJ7kvnfRKJa/images/managed-postgres/pgpg/post-migration/pause-pipe.webp?fit=max&auto=format&n=y9c9rzJ7kvnfRKJa&q=85&s=6d650c0e80230dd2fec9d6cce10d79de" alt="移行後の手順 3: pause the ClickPipe to stop replication" size="lg" border width="1408" height="1322" data-path="images/managed-postgres/pgpg/post-migration/pause-pipe.webp" />

<h3 id="cutover-reset-sequences">
  ステップ4: シーケンスをリセットする
</h3>

以降の挿入が正しい値から続くように、宛先側でシーケンスをリセットします。**Reset sequences** をクリックすると、各シーケンスが対象テーブル内の現在の最大値に合わせて調整されます。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/y9c9rzJ7kvnfRKJa/images/managed-postgres/pgpg/post-migration/reset-sequences.webp?fit=max&auto=format&n=y9c9rzJ7kvnfRKJa&q=85&s=6df042af5d04372c4f3ea93ebc572073" alt="移行後の手順 4: reset sequences on the destination" size="lg" border width="1382" height="1196" data-path="images/managed-postgres/pgpg/post-migration/reset-sequences.webp" />

<h3 id="cutover-traffic">
  ステップ 5: トラフィックを切り替える
</h3>

アプリケーションのデータベース URL を Managed Postgres の接続文字列に変更し、読み取りと書き込みを Managed Postgres サービスに向けます。ウィザードでは接続情報が複数のフォーマット (**url**、**psql**、**env**、**yaml**、**jdbc**) で提供されるため、ご利用のスタックに合ったものをコピーしてください。アプリケーションの更新が完了したら、**Mark as completed** をクリックします。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/y9c9rzJ7kvnfRKJa/images/managed-postgres/pgpg/post-migration/cutover-traffic.webp?fit=max&auto=format&n=y9c9rzJ7kvnfRKJa&q=85&s=a9ac993e5c1d9de925668c7baf169b42" alt="移行後の手順 5: トラフィックを切り替える using the Managed Postgres connection string" size="lg" border width="1486" height="1018" data-path="images/managed-postgres/pgpg/post-migration/cutover-traffic.webp" />

<h3 id="cutover-cleanup">
  ステップ6: クリーンアップ
</h3>

カットオーバーが完了し、新しいサービスが正常に稼働していることを確認したら、移行を削除してソース側のリソースを解放します。ウィザードには、スロット名があらかじめ入力された `pg_drop_replication_slot` コマンドが表示されます。これをソース側で実行して、レプリケーションスロットを削除してください:

```sql theme={null}
SELECT pg_drop_replication_slot('<slot_name>');
```

続いて **Delete ClickPipe migration** をクリックし、**データソース** から移行を削除します。

<Image img="https://mintcdn.com/private-7c7dfe99-parallel-read-in-order-multi-part/y9c9rzJ7kvnfRKJa/images/managed-postgres/pgpg/post-migration/cleanup.webp?fit=max&auto=format&n=y9c9rzJ7kvnfRKJa&q=85&s=bd1fc0408e1dd1b552f66cfc2f1e980e" alt="移行後の手順 6: clean up by dropping the replication slot and deleting the migration" size="lg" border width="1452" height="1432" data-path="images/managed-postgres/pgpg/post-migration/cleanup.webp" />

<h2 id="next-steps">
  次のステップ
</h2>

* [Managed Postgres クイックスタート](/ja/products/managed-postgres/quickstart)
* [Managed Postgres の接続情報](/ja/products/managed-postgres/connection)
* [ClickPipes Postgres のよくある質問](/ja/integrations/clickpipes/postgres/faq)
