---
title: "Postgres to ClickHouse: Data Modeling Tips"
description: "Postgres tables replicated to ClickHouse via PeerDB land in ReplacingMergeTree tables where updates and deletes arrive as versioned inserts (_peerdb_version, _peerdb_is_deleted), so duplicates persist until background merges run."
url: "https://swe.quest/articles/postgres-to-clickhouse-data-modeling-tips-aec1f81e14af15d4"
canonical: "https://clickhouse.com/blog/postgres-to-clickhouse-data-modeling-tips"
source: "ClickHouse"
published: "2024-08-28"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["analytics","postgres","event-driven","sql","replication"]
---

# Postgres to ClickHouse: Data Modeling Tips

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2024-08-28, 9 min read, How-to. Original: [https://clickhouse.com/blog/postgres-to-clickhouse-data-modeling-tips](https://clickhouse.com/blog/postgres-to-clickhouse-data-modeling-tips)

## Summary

Postgres tables replicated to ClickHouse via PeerDB land in ReplacingMergeTree tables where updates and deletes arrive as versioned inserts (\_peerdb\_version, \_peerdb\_is\_deleted), so duplicates persist until background merges run. Deduplicate at query time with FINAL, argMax, ROW\_NUMBER window functions or views wrapping them, and hide deleted rows with row policies. Pick ordering keys from frequently filtered columns in ascending cardinality, keep PRIMARY KEY a prefix of ORDER BY, and remember ClickHouse's primary key is a non-unique sparse index rather than a B-tree. Columns need explicit Nullable types, and ordering keys can be changed via materialized views or by predefining and swapping target tables.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [Event-driven architecture](https://swe.quest/tags/event-driven.md) (Backend & APIs)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Replication](https://swe.quest/tags/replication.md) (Distributed systems)

## Prerequisites

- PostgreSQL basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- SQL ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- change data capture concepts

## Keywords

ClickHouse, PeerDB, ReplacingMergeTree, PostgreSQL, FINAL, argMax

## Related posts

- [Postgres to ClickHouse: Data Modeling Tips V2](https://swe.quest/articles/postgres-to-clickhouse-data-modeling-tips-v2-421af33ac59693d5.md): ClickHouse · 2025-03-06 · Medium · 21 min read · Analytics & OLAP, PostgreSQL, Event-driven architecture, Replication, SQL
  Postgres CDC into ClickHouse (via PeerDB or ClickPipes) uses logical decoding and maps tables to ReplacingMergeTree.
- [Change Data Capture (CDC) with PostgreSQL and ClickHouse - Part 2](https://swe.quest/articles/change-data-capture-cdc-with-postgresql-and-clickhouse-part-2-2123f803752f2e9d.md): ClickHouse · 2023-06-15 · Medium · 22 min read · Event-driven architecture, PostgreSQL, Analytics & OLAP, Data pipelines, Replication
  A Postgres-to-ClickHouse CDC pipeline is built with Debezium, Kafka Connect and a ClickHouse materialized view, keeping a ReplacingMergeTree table in sync with the 28M-row UK property price dataset under a mix of inserts, updates and deletes.
- [Change Data Capture (CDC) with PostgreSQL and ClickHouse - Part 1](https://swe.quest/articles/change-data-capture-cdc-with-postgresql-and-clickhouse-part-1-10e3d601e2748dab.md): ClickHouse · 2023-06-15 · Medium · 23 min read · Event-driven architecture, PostgreSQL, Analytics & OLAP, Replication, Queues & messaging
  Postgres-to-ClickHouse CDC can be built by streaming row changes from Postgres logical decoding (pgoutput plugin, replication slots, LSN ordering) through Debezium and Kafka, then applying them in ClickHouse with the ReplacingMergeTree engine.
- [PostgreSQL + ClickHouse as the Open Source unified data stack](https://swe.quest/articles/postgresql-clickhouse-as-the-open-source-unified-data-stack-2760f19ba52907df.md): ClickHouse · 2026-01-27 · Easy · 5 min read · PostgreSQL, Analytics & OLAP, Event-driven architecture, Replication
  An open source stack pairs PostgreSQL as the transactional system of record with ClickHouse for analytics, connected by PeerDB for change data capture and the pg\_clickhouse extension, which transparently offloads analytical queries from Postgres to ClickHouse.
- [Postgres CDC in ClickHouse, A year in review](https://swe.quest/articles/postgres-cdc-in-clickhouse-a-year-in-review-6b9d350bcade98d4.md): ClickHouse · 2025-12-03 · Medium · 13 min read · PostgreSQL, Event-driven architecture, Data pipelines, Analytics & OLAP, Replication
  A year after PeerDB's acquisition, Postgres CDC into ClickHouse via ClickPipes has grown to 400+ companies replicating over 200 TB per month, driven by real-time customer-facing analytics and data warehousing workloads.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
