> ## 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.

# 使用 ClickPipes 同步到 ClickHouse

> 利用内置的 CDC（变更数据捕获）能力将 Postgres 数据复制到 ClickHouse

[Beta](https://clickhouse.com/cloud/postgres)

每个 ClickHouse Managed Postgres 实例都内置了面向任意 ClickHouse 服务的 CDC (变更数据捕获) 能力。借助该能力，你可以将 Postgres 实例上的部分或全部数据迁移到 ClickHouse，并让 Postgres 上的数据变更持续、近乎实时地同步到 ClickHouse。其底层由 [ClickPipes](https://clickhouse.com/docs/zh/integrations/clickpipes/home.md) 提供支持。

要使用该功能，请在 Postgres 实例的侧边栏中点击 **Sync to ClickHouse**。

![在侧边栏中显示集成选项的 ClickHouse 集成落地页](https://mintcdn.com/private-7c7dfe99/7bwsXgjDg4ADP__Z/images/managed-postgres/clickhouse-integration-intro.webp?fit=max\&auto=format\&n=7bwsXgjDg4ADP__Z\&q=85\&s=e32d61f84fe019d735b5f18a6ba5ae47)

> [!NOTE]
>
> 在继续之前，请确保 ClickPipes 服务能够访问你的 Postgres 服务。默认情况下应当如此，但如果你限制了 IP 访问，则可能需要根据 **ClickHouse 服务**所在的区域，从[此列表](https://clickhouse.com/docs/zh/integrations/clickpipes/networking/static-ips.md)中为相应的源 IP 授予访问权限。

点击 **Replicate data in ClickHouse**，开始设置你的 ClickPipe。

**1. 配置复制服务**

填写复制相关设置：

* **Integration name**：此 ClickPipe 的名称
* **ClickHouse 服务**：选择已有的 ClickHouse Cloud service，或新建一个
* **Postgres database**：作为复制来源的源数据库
* **Replication method**：选择以下之一：
  * \*\*Initial load + CDC (变更数据捕获) \*\*：导入现有数据，并持续同步新的变更 (推荐)
  * **Initial load only**：对现有数据做一次性 snapshot，不进行后续同步
  * **CDC (变更数据捕获)  only**：跳过初始 snapshot，仅捕获此后产生的新变更

![复制服务配置界面，显示集成名称、目标端 service 和复制方式选项](https://mintcdn.com/private-7c7dfe99/7bwsXgjDg4ADP__Z/images/managed-postgres/replication-service-step.webp?fit=max\&auto=format\&n=7bwsXgjDg4ADP__Z\&q=85\&s=3a4e607c9b176e79ecd13910e1498e6c)

点击 **Next** 继续。

**2. 配置复制设置**

微调数据的复制方式：

* **Sync interval (seconds)**：从 Postgres 拉取变更的频率 (默认值：60)
* **Parallel threads for initial load**：初始 snapshot 期间使用的线程数 (默认值：4)
* **Pull batch size**：复制过程中每个批次拉取的行数 (默认值：100000)
* **Snapshot number of rows per partition**：初始 snapshot 期间每个分区的行数 (默认值：100000)
* **Snapshot number of tables in parallel**：并发执行 snapshot 的表数量 (默认值：1)

![复制设置步骤，显示同步间隔、并行线程数、拉取批次大小和 snapshot 配置选项](https://mintcdn.com/private-7c7dfe99/7bwsXgjDg4ADP__Z/images/managed-postgres/integration-replication-settings.webp?fit=max\&auto=format\&n=7bwsXgjDg4ADP__Z\&q=85\&s=352b1f09ca383bb75d9553ba0def5f77)

点击 **Next** 继续。

**3. 选择要复制的表**

选择目标端数据库，并选定要复制的表：

* **Destination database**：选择已有的 ClickHouse 数据库，或新建一个
* **Prefix default destination table names with schema name**：将 Postgres schema 名作为表名前缀，以避免命名冲突
* **Preserve NULL values from source**：保留 NULL 值，而不是转换为类型默认值。该设置对管道中的所有表生效，且管道创建后无法更改。
* **Remove deleted rows during merges**：对于 [ReplacingMergeTree](https://clickhouse.com/docs/zh/reference/engines/table-engines/mergetree-family/replacingmergetree.md) 表，在后台 merge 期间物理删除已删除的行

展开 schema 并选择需要复制的具体表。你还可以自定义目标端表名和列设置。

![选择表步骤，显示数据库选择、复制选项以及按 schema 分组的表选择器](https://mintcdn.com/private-7c7dfe99/1Nz1dAkMe4yV28t-/images/managed-postgres/select-tables-step.webp?fit=max\&auto=format\&n=1Nz1dAkMe4yV28t-\&q=85\&s=e0e4d8bcb77b0565490b12252841ccbe)

点击 **Replicate data to ClickHouse** 开始复制。

**4. 监控你的 ClickPipe**

ClickPipe 启动后，你会在同一菜单中看到它。全部数据的初始 snapshot 可能需要一段时间，具体取决于表的规模。

![ClickHouse 集成列表，显示一个正在运行的 ClickPipe 及其目标端 service 和状态](https://mintcdn.com/private-7c7dfe99/1Nz1dAkMe4yV28t-/images/managed-postgres/integration-running.webp?fit=max\&auto=format\&n=1Nz1dAkMe4yV28t-\&q=85\&s=487881b858fdac6860c3ad67b0ea66e3)

点击集成名称即可查看详细状态、监控进度、查看错误并管理该 ClickPipe。参见 [Postgres ClickPipe 的生命周期](https://clickhouse.com/docs/zh/integrations/clickpipes/postgres/lifecycle.md)，了解 ClickPipe 可能处于的各种状态。
