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

# ClickHouse 集成

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

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}>
                <span>Beta</span>
            </a>;
  }
  return <a href="/zh/reference/settings/beta-and-experimental-features#beta-features" className="betaBadge">
            <span>Beta 版功能</span>
        </a>;
};

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

每个 Managed Postgres 实例都内置了向任意 ClickHouse 服务进行 CDC (变更数据捕获) 的能力。这使你能够将 Postgres 实例中的部分或全部数据迁移到 ClickHouse，并让 Postgres 中的数据变更持续、近乎实时地同步到 ClickHouse。其底层由 [ClickPipes](/zh/integrations/clickpipes/home) 提供支持。

要使用此功能，请点击 Postgres 实例侧边栏中的 **ClickHouse 集成**。

<Image img="https://mintcdn.com/private-7c7dfe99-vortex-format/xHfTFhAoB3hIpGFb/images/managed-postgres/clickhouse-integration-intro.webp?fit=max&auto=format&n=xHfTFhAoB3hIpGFb&q=85&s=f2de966f7661a462fbb9a0a310bbb270" alt="显示侧边栏中集成选项的 ClickHouse 集成页面" size="md" border width="1526" height="956" data-path="images/managed-postgres/clickhouse-integration-intro.webp" />

<Note>
  在继续之前，请确保你的 Postgres 服务可被 ClickPipes 服务访问。默认情况下通常已满足此条件，但如果你限制了 IP 访问，则可能需要根据你的 **ClickHouse 服务** 所在的区域，从[此](/zh/integrations/clickpipes/home#list-of-static-ips)列表中为某些源 IP 开放访问。
</Note>

点击 **在 ClickHouse 中复制数据**，开始设置你的 ClickPipe。

<Steps>
  <Step title="配置复制服务" id="configure-replication-service">
    填写复制设置：

    * **集成名称**：此 ClickPipe 的名称
    * **ClickHouse 服务**：选择现有的 ClickHouse Cloud 服务或创建一个新的
    * **Postgres 数据库**：要从中复制的源数据库
    * **复制方法**：选择以下之一：
      * **初始加载 + CDC (变更数据捕获)**：导入现有数据，并使用后续变更持续更新表 (推荐)
      * **仅初始加载**：对现有数据执行一次性快照，不进行持续更新
      * **仅 CDC (变更数据捕获)**：跳过初始快照，仅捕获后续变更

    <Image img="https://mintcdn.com/private-7c7dfe99-vortex-format/xHfTFhAoB3hIpGFb/images/managed-postgres/replication-service-step.webp?fit=max&auto=format&n=xHfTFhAoB3hIpGFb&q=85&s=71c792ea56ab5ed02222c2979498ae84" alt="复制服务配置，显示集成名称、目标服务和复制方法选项" size="md" border width="1620" height="956" data-path="images/managed-postgres/replication-service-step.webp" />

    Click **下一步** 继续。
  </Step>

  <Step title="配置复制设置" id="replication-settings">
    微调数据复制方式：

    * **同步间隔 (秒)**：从 Postgres 拉取变更的频率 (默认：60)
    * **初始加载的并行线程数**：初始快照期间使用的线程数 (默认：4)
    * **拉取批次大小**：复制期间每个批次拉取的行数 (默认：100000)
    * **快照中每个分区的行数**：初始快照期间每个分区包含的行数 (默认：100000)
    * **并行快照的表数量**：并发执行快照的表数量 (默认：1)

    <Image img="https://mintcdn.com/private-7c7dfe99-vortex-format/xHfTFhAoB3hIpGFb/images/managed-postgres/integration-replication-settings.webp?fit=max&auto=format&n=xHfTFhAoB3hIpGFb&q=85&s=f73c47e084727b55d05755db6127c896" alt="复制设置步骤，显示同步间隔、并行线程、拉取批次大小和快照配置选项" size="md" border width="1526" height="957" data-path="images/managed-postgres/integration-replication-settings.webp" />

    Click **下一步** 继续。
  </Step>

  <Step title="选择要复制的表" id="select-tables">
    选择目标数据库，并选择要复制的表：

    * **目标数据库**：选择现有的 ClickHouse 数据库或创建一个新的
    * **默认目标表名加上 schema 名称前缀**：添加 Postgres schema 作为前缀，以避免命名冲突
    * **保留源中的 NULL 值**：保留 NULL 值，而不是将其转换为类型默认值。此设置适用于管道中的所有表，并且在管道创建后无法更改。
    * **在合并期间移除已删除的行**：对于 [ReplacingMergeTree](/zh/reference/engines/table-engines/mergetree-family/replacingmergetree) 表，会在后台合并期间物理删除已删除的行

    展开各个 schema 并选择要复制的单个表。你还可以自定义目标表名和列设置。

    <Image img="https://mintcdn.com/private-7c7dfe99-vortex-format/xHfTFhAoB3hIpGFb/images/managed-postgres/select-tables-step.webp?fit=max&auto=format&n=xHfTFhAoB3hIpGFb&q=85&s=be0345181d3022080ad8653542714421" alt="选择表步骤，显示数据库选择、复制选项，以及按 schema 分组的表选择器" size="md" border width="2454" height="1804" data-path="images/managed-postgres/select-tables-step.webp" />

    Click **将数据复制到 ClickHouse** 开始复制。
  </Step>

  <Step title="监控你的 ClickPipe" id="monitor-clickpipe">
    ClickPipe 启动后，你会在同一菜单中看到它。所有数据的初始快照可能需要一些时间，具体取决于你的表大小。

    <Image img="https://mintcdn.com/private-7c7dfe99-vortex-format/xHfTFhAoB3hIpGFb/images/managed-postgres/integration-running.webp?fit=max&auto=format&n=xHfTFhAoB3hIpGFb&q=85&s=37b194710421e11fdbeaf84474491c60" alt="ClickHouse 集成列表，显示一个处于运行中的 ClickPipe 及其目标服务和状态" size="md" border width="2518" height="622" data-path="images/managed-postgres/integration-running.webp" />

    点击集成名称可查看详细状态、监控进度、查看错误并管理 ClickPipe。参见 [Postgres ClickPipe 的生命周期](/zh/integrations/clickpipes/postgres/lifecycle) 来了解 ClickPipe 可能处于的不同状态。
  </Step>
</Steps>
