Configure dbt to build and manage ClickHouse tables and views from declarative SQL models.
dbt brings version-controlled, testable transformations to ClickHouse’s lightning-fast analytics engine. Together they let analysts deploy reproducible pipelines without writing orchestration code.
Install the official dbt-adapter: pip install dbt-clickhouse
. The package ships with a SQLAlchemy driver so no separate ODBC layer is needed.
Create a profile keyed by project name. Supply host, port, database, and optional user credentials. dbt will open HTTP connections on port 8123 by default.
profiles.yml
entry
clickhouse_project:
target: dev
outputs:
dev:
type: clickhouse
host: localhost
port: 8123
database: default
schema: analytics
user: default
password: ''
threads: 4
Place a file like models/orders.sql
and reference the source tables.
-- models/orders.sql
select o.id as order_id,
c.name as customer_name,
o.order_date,
o.total_amount,
sum(oi.quantity) as items_count
from {{ source('raw', 'Orders') }} o
join {{ source('raw', 'Customers') }} c on c.id=o.customer_id
join {{ source('raw', 'OrderItems') }} oi on oi.order_id=o.id
group by all
Use table
when you want dbt to CREATE TABLE … ENGINE = MergeTree. Choose view
for lightweight virtualization or incremental
when daily partitions land in ClickHouse.
dbt run
compiles models and pushes SQL to ClickHouse. dbt test
validates assumptions like unique
primary keys in Customers.
Partition large tables (e.g., on order_date
) to keep merges quick. Use materialized views for roll-ups. Commit generated DDL to git for reproducibility.
Using view
for huge aggregations. Views render at query time and can exhaust memory. Switch to table
materialization.
Ignoring data types in models. ClickHouse infers types; unexpected Nullable
columns appear. Cast explicitly inside SELECT or use dbt config(column_types=...)
.
dbt Cloud, Airflow, or GitHub Actions invoke dbt run
on every commit, rebuilding ClickHouse tables automatically.
Add tests, enable incremental
materializations, and monitor query logs to fine-tune engines and partitions.
Yes. The adapter backs several high-traffic workloads, though MergeTree tuning remains your responsibility.
Absolutely. Define a unique_key and partition condition. dbt will append new records without full table rebuilds.