Use clickhouse_fdw to query Azure-hosted ClickHouse tables directly from PostgreSQL.
ClickHouse excels at large-scale analytics; PostgreSQL handles OLTP. A foreign data wrapper (FDW) lets you join both worlds without ETL or duplicated storage.
Azure-deployed ClickHouse endpoint, Postgres 13+ with clickhouse_fdw, open port 9440, and valid ClickHouse credentials or Azure AD token.
Run CREATE EXTENSION IF NOT EXISTS clickhouse_fdw;
. If missing, install postgresql-clickhouse-fdw
via package manager or compile from source, then retry.
CREATE SERVER clickhouse_srv
FOREIGN DATA WRAPPER clickhouse_fdw
OPTIONS (
host 'ch-eastus-01.a.database.azure.com',
port '9440',
dbname 'ecommerce',
secure 'true'
);
CREATE USER MAPPING FOR app_user
SERVER clickhouse_srv
OPTIONS (user 'ch_app', password 'REDACTED');
IMPORT FOREIGN SCHEMA public
LIMIT TO (customers, orders, orderitems, products)
FROM SERVER clickhouse_srv INTO public;
SELECT c.name,
SUM(oi.quantity * p.price) AS lifetime_value
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN orderitems oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
GROUP BY c.name
ORDER BY lifetime_value DESC
LIMIT 10;
Set clickhouse_fdw.use_remote_estimate = on
, align ClickHouse partitions with WHERE clauses, avoid SELECT *, and use LIMIT to cut transfer costs.
Enable log_fdw_detail
, watch Azure Monitor for CPU/latency, and test network RTT—keep it <50 ms for stable query times.
No. clickhouse_fdw is read-only. Perform inserts via ClickHouse native interfaces or Kafka-engine tables.
Yes. Set secure 'true'
and ensure port 9440 is open. Certificates are validated by libcurl.
Keep use_remote_estimate = on
and avoid PostgreSQL functions in WHERE clauses. Stick to simple comparisons and arithmetic.