Validate a Postgres to ClickHouse migration
By Tanin Na Nakorn · Updated
Migrations fail quietly. A batch times out and gets skipped. A time zone shifts a day's worth of rows into the next
day. A Float32 rounds the cents away. The row counts look close enough, and the problem shows up weeks
later as a number on a dashboard nobody trusts.
The example checks an orders table copied from Postgres to ClickHouse. The checks go from cheap to
thorough, and you only go deeper where something doesn't match.
1. Connect both databases
In Superintendent, click + next to Databases and add the Postgres database, then the ClickHouse one. Both show up in the sidebar with their tables.
2. Compare counts and totals per day
Run the same aggregate on each database. Each query runs on its own server, so even a billion-row table returns just a few hundred rows.
select
created_at::date as day,
count(*) as orders,
sum(amount_cents) as amount_cents,
count(distinct customer_id) as customers
from orders
group by 1
select
toDate(created_at) as day,
count() as orders,
sum(amount_cents) as amount_cents,
uniqExact(customer_id) as customers
from orders
group by day
Click Download to Workspace above each result. Now both summaries are tables in the local workspace, and you can compare them in one query:
select
coalesce(p.day, c.day) as day,
p.orders as pg_orders,
c.orders as ch_orders,
p.amount_cents - c.amount_cents as amount_diff,
p.customers - c.customers as customers_diff
from pg_daily p
full outer join ch_daily c on c.day = p.day
where p.day is null
or c.day is null
or p.orders is distinct from c.orders
or p.amount_cents is distinct from c.amount_cents
or p.customers is distinct from c.customers
order by day
| day | pg_orders | ch_orders | amount_diff | customers_diff | |
|---|---|---|---|---|---|
| 1 | 2025-06-14 | 41892 | 41392 | 1928400 | 488 |
| 2 | 2025-09-30 | 39011 | 38774 | 861150 | 230 |
| 3 | 2025-10-01 | 40275 | 40512 | -861150 | -230 |
An empty result means every day matches. Here there are two different problems:
- June 14 is missing exactly 500 orders in ClickHouse. Probably a skipped batch.
- September 30 and October 1 cancel each other out: 237 orders moved from one day to the next. That's a time zone difference, not lost data.
3. Find the exact rows
Now download the rows, but only for the day that's off. Run on each database, and download both results:
select id, customer_id, amount_cents, status, created_at
from orders
where created_at >= '2025-06-14' and created_at < '2025-06-15'
select id, customer_id, amount_cents, status, created_at
from orders
where created_at >= '2025-06-14' and created_at < '2025-06-15'
select p.*
from pg_jun14 p
left join ch_jun14 c on c.id = p.id
where c.id is null
order by p.id
Scroll through the 500 rows in the grid. Sort by id: if they're one contiguous range, it's a single
failed batch, and you know exactly which IDs to backfill.
4. Compare values, not just rows
Rows can all be present and still be wrong. Join on the key and compare the columns that matter:
select p.id, p.amount_cents, c.amount_cents, p.status, c.status
from pg_jun14 p
join ch_jun14 c on c.id = p.id
where p.amount_cents is distinct from c.amount_cents
or p.status is distinct from c.status
5. Save the checks
Save the workspace as a .super file. It keeps both connections and every query. After the backfill,
open it, rerun the two daily queries, download them again, and check that daily_differences comes back
empty.
Comparing exports instead of live databases? See Compare two CSV files. More on each database: PostgreSQL, ClickHouse.
Questions
Do I have to download the whole table?
No. Start with counts and sums computed on each database, which return a few rows. Download rows only for the slice that doesn't match, like one day or one customer.
Why do timestamps differ between Postgres and ClickHouse?
Usually time zones or precision. ClickHouse DateTime has second precision unless you use DateTime64, and it may store values in a different time zone. Truncate or convert both sides the same way before comparing.
Does this work for other databases?
Yes. The same steps work between any two of PostgreSQL, ClickHouse, Snowflake, and SQLite, or between a database and a CSV export.
Can I rerun the checks after the next backfill?
Yes. Save the workspace as a .super file. It keeps both connections and every query, so you can rerun them in a few clicks.