> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 ClickHouse Cloud 迁移到 Managed Postgres

> 了解如何使用 ClickHouse Cloud 内置的数据源导入向导，将您的 PostgreSQL 数据库迁移到 ClickHouse Managed Postgres。

export const Image = ({img, alt, size = "lg"}) => {
  const normalizedSize = ["sm", "md", "lg"].includes(size) ? size : "lg";
  return <div className={`ch-image-${normalizedSize}`}>
      <Frame>
        <img src={img} alt={alt} />
      </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}>
                <Icon />
                <span>Beta</span>
            </a>;
  }
  return <div className="betaBadge">
            <Icon />
            <span>
                Beta 版功能。 
                <u>
                    <a href="/docs/docs/beta-and-experimental-features#beta-features">
                        了解更多。
                    </a>
                </u>
            </span>
        </div>;
};

<BetaBadge />

ClickHouse Cloud 内置了导入向导，可将您的外部 PostgreSQL 数据库迁移到 Managed Postgres 服务。该向导通过五个引导步骤完成源连接、schema 导出和导入、复制设置以及表选择。

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

* 可使用具有复制权限的用户访问源 PostgreSQL 数据库。
* 准备一个 ClickHouse Managed Postgres 服务作为迁移目标。如果你还没有，请参阅[快速入门](/docs/zh/products/managed-postgres/quickstart)。
* 在本地计算机上安装 `pg_dump` 和 `psql`。两者都包含在标准 PostgreSQL 客户端工具中。

<div id="considerations">
  ## 迁移前的注意事项
</div>

* **DDL 传播**：持续复制 (CDC) 会捕获 DML 操作以及 `ADD COLUMN`。其他 DDL 变更 (如 `DROP COLUMN` 和 `ALTER COLUMN`) 不会自动传播，必须在目标端手动执行。

<Steps>
  <Step title="连接到源数据库" id="step-1-connect">
    打开 [ClickHouse Cloud 控制台](https://clickhouse.cloud)，然后选择您的 Managed Postgres 服务。

    <Image img="https://mintcdn.com/private-7c7dfe99/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/servicecard.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=9b98290197063c76fdacc9ac06749bea" 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/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/overview.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=f22839ab1704fb125a6b0e6efb871934" 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/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/startimport.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=e65b851af7a659ca5c994c540d337833" 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/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/migrationform.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=4e668ee6f701c07181d5efc2148f9d68" alt="步骤 1：包含摄取方式选项的源数据库连接表单" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/migrationform.webp" />

    点击 **Next**。
  </Step>

  <Step title="导出数据库 schema" id="step-2-export-schema">
    向导会显示一条已预填源连接信息的 `pg_dump` 命令。在终端中运行它：

    <Image img="https://mintcdn.com/private-7c7dfe99/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/nextexport.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=3b134690c48bbd56aaa7e186348b7f61" alt="步骤 2：用于导出 schema 的 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/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/psqlexport.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=a3a03aa81fbb7bf54e26c06d1f34a54a" alt="运行 pg_dump 后的终端输出" size="lg" border width="1452" height="422" data-path="images/managed-postgres/pgpg/psqlexport.webp" />

    点击 **Next**。
  </Step>

  <Step title="将 schema 导入到 Managed Postgres 服务" id="step-3-import-schema">
    从下拉列表中选择目标数据库，或点击 **Create a new database** 来创建一个新数据库。

    向导会显示一条 `psql` 命令，用于将 schema 转储应用到您的 Managed Postgres 服务。在终端中运行它：

    <Image img="https://mintcdn.com/private-7c7dfe99/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/nextimport.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=987b248bd1bef286ce2e2f44ecddacf7" alt="步骤 3：用于导入 schema 的 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/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/psqlimport.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=21488b23372606d9a878773443840952" alt="运行 psql 导入 schema 后的终端输出" size="lg" border width="2362" height="762" data-path="images/managed-postgres/pgpg/psqlimport.webp" />

    点击 **Next**。
  </Step>

  <Step title="配置摄取设置" id="step-4-ingestion-settings">
    指定逻辑复制要使用的 publication。如果将此项留空，系统会自动创建一个 publication。

    展开 **Advanced replication settings** 以调整吞吐量：

    | 设置                                    | 默认值     | 说明                      |
    | ------------------------------------- | ------- | ----------------------- |
    | Sync interval (seconds)               | 10      | 轮询 replication slot 的频率 |
    | Parallel threads for initial load     | 4       | 初始批量复制阶段使用的线程数          |
    | Pull batch size                       | 100,000 | 每个复制批次拉取的行数             |
    | Snapshot number of rows per partition | 100000  | 大表快照中每个分区的大小            |
    | Snapshot number of tables in parallel | 1       | 并行创建快照的表数量              |

    <Image img="https://mintcdn.com/private-7c7dfe99/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/advancedsettings.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=aaa8c65ce3951d743cda7b512b4f37d9" alt="步骤 4：包含 publication 和高级复制选项的摄取设置表单" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/advancedsettings.webp" />

    点击 **Next**。
  </Step>

  <Step title="选择表" id="step-5-select-tables">
    选择要复制的表。表按 schema 分组。您可以选择单个表，也可以展开某个 schema 以选择其中的所有表。

    <Image img="https://mintcdn.com/private-7c7dfe99/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/tablepicker.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=445729d818e3d464f58257282116d7f7" alt="步骤 5：按 schema 分组并带有 Create migration 按钮的表选择器" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/tablepicker.webp" />

    点击 **Create migration**。
  </Step>
</Steps>

<div id="monitor">
  ## 监控迁移
</div>

创建迁移后，你会在**数据源**中看到该迁移，状态为 **运行中**。

<Image img="https://mintcdn.com/private-7c7dfe99/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/migrationlist.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=d4c7643df720b32f247ade727c8559ed" alt="显示正在运行迁移的数据源列表" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/migrationlist.webp" />

点击该迁移即可打开详情视图。**表**选项卡会显示每个表的初始加载进度，包括已处理的行数、分区数以及每个分区的平均耗时。**指标**选项卡会在 CDC (变更数据捕获)  开始后显示复制延迟和吞吐量。

<Image img="https://mintcdn.com/private-7c7dfe99/-DTs8Nf-Dydrn3iN/images/managed-postgres/pgpg/initialload.webp?fit=max&auto=format&n=-DTs8Nf-Dydrn3iN&q=85&s=2b1fc16a14dccd3685665b0119a4bf41" alt="显示各表初始加载统计信息的迁移详情视图" size="lg" border width="3680" height="2392" data-path="images/managed-postgres/pgpg/initialload.webp" />

<div id="post-migration">
  ## 迁移后的任务
</div>

完成初始加载后，如果使用了 CDC，且复制延迟接近于零：

**验证行数。** 在切换流量之前，对源端和目标端的关键表进行抽样核对：

```sql theme={null}
SELECT COUNT(*) FROM public.orders;
```

**停止向源库写入。** 暂停应用写入。要在切换期间强制设为只读模式：

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

**确认复制已追平。** 比较源端和目标端的最新一行：

```sql theme={null}
-- 在源端和目标端均执行
SELECT MAX(id), MAX(updated_at) FROM public.orders;
```

**重置序列。** 使序列与各表中的当前最大值保持一致：

```sql theme={null}
DO $$
DECLARE r RECORD;
BEGIN
    FOR r IN
        SELECT
            n.nspname AS schema_name,
            c.relname AS table_name,
            a.attname AS column_name,
            pg_get_serial_sequence(format('%I.%I', n.nspname, c.relname), a.attname) AS seq_name
        FROM pg_class c
        JOIN pg_namespace n ON n.oid = c.relnamespace
        JOIN pg_attribute a ON a.attrelid = c.oid
        WHERE c.relkind = 'r'
            AND a.attnum > 0
            AND NOT a.attisdropped
            AND n.nspname NOT IN ('pg_catalog', 'information_schema')
    LOOP
        IF r.seq_name IS NOT NULL THEN
            EXECUTE format(
                'SELECT setval(%L, COALESCE((SELECT MAX(%I) FROM %I.%I), 0) + 1, false)',
                r.seq_name, r.column_name, r.schema_name, r.table_name
            );
        END IF;
    END LOOP;
END $$;
```

**切换应用流量。** 将读取和写入流量切换到你的 Managed Postgres 服务，并监控错误、约束冲突以及复制运行状况。

**清理。**  完成切换并确认新服务运行正常后，从**数据源**中删除该迁移。如果你使用了 CDC，请从源端删除 replication slot 以释放资源：

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

<div id="next-steps">
  ## 后续步骤
</div>

* [Managed Postgres 快速入门](/docs/zh/products/managed-postgres/quickstart)
* [Managed Postgres 连接信息](/docs/zh/products/managed-postgres/connection)
* [ClickPipes Postgres FAQ](/docs/zh/integrations/clickpipes/postgres/faq)
