Skip to content

ClickHouse/pg_clickhouse

Repository files navigation

pg_clickhouse Postgres Extension

PGXN Docker GitHub Postgres ClickHouse

This library contains pg_clickhouse, a PostgreSQL extension that runs analytics queries on ClickHouse right from PostgreSQL without rewriting any SQL. It supports PostgreSQL 13 and later and ClickHouse v23 and later.

Getting Started

The simplest way to try pg_clickhouse is the Docker image, which contains the standard PostgreSQL Docker image with the pg_clickhouse and re2 extensions:

docker run --name pg_clickhouse -e POSTGRES_PASSWORD=my_pass \
       -d ghcr.io/clickhouse/pg_clickhouse:18
docker exec -it pg_clickhouse psql -U postgres -c 'CREATE EXTENSION pg_clickhouse'

See the tutorial to get started importing ClickHouse tables and pushing down queries.

Documentation

Test Case: TPC-H

This table compares TPC-H query performance between regular PostgreSQL tables and pg_clickhouse connected to ClickHouse, both loaded at scaling factor 1; ✔︎ indicates full pushdown as a single foreign scan and ✼ indicates full pushdown as multiple foreign scans. All tests run on a MacBook Pro M4 Max with 36 GB of memory.

Query PostgreSQL pg_clickhouse Pushdown
Query 1 4483 ms 59 ms
Query 2 588 ms 24 ms
Query 3 786 ms 62 ms
Query 4 550 ms 38 ms
Query 5 721 ms 1439 ms
Query 6 592 ms 17 ms
Query 7 639 ms 29 ms
Query 8 398 ms 383 ms
Query 9 2842 ms 162 ms
Query 10 860 ms 125 ms
Query 11 276 ms 21 ms
Query 12 963 ms 26 ms
Query 13 1037 ms 1354 ms
Query 14 675 ms 30 ms
Query 15 2520 ms 387 ms
Query 16 539 ms 823 ms
Query 17 2107 ms 37 ms
Query 18 5230 ms 7228 ms
Query 19 68 ms 47 ms
Query 20 473 ms 28 ms
Query 21 1145 ms 4470 ms
Query 22 270 ms 45 ms

Compile From Source

General Unix

The PostgreSQL and curl development packages include pg_config and curl-config in the path, so you should be able to just run make (or gmake), then make install, then in your database CREATE EXTENSION pg_clickhouse.

Debian / Ubuntu / APT

See PostgreSQL Apt for details on pulling from the PostgreSQL Apt repository.

sudo apt install \
  postgresql-server-19 \
  libcurl4-openssl-dev \
  uuid-dev \
  liblz4-dev \
  libzstd-dev \
  libssl-dev \
  make \
  gcc

RedHat / CentOS / Yum

sudo yum install \
  postgresql-server \
  libcurl-devel \
  libuuid-devel \
  lz4-devel \
  libzstd-devel \
  openssl-libs \
  automake \
  gcc

See PostgreSQL Yum for details on pulling from the PostgreSQL Yum repository.

Install From PGXN

With the above dependencies satisfied use the PGXN client (available as Homebrew, Apt and Yum packages named pgxnclient) to download, compile, and install pg_clickhouse:

pgxn install pg_clickhouse

Compile and Install

To build and install the ClickHouse library and pg_clickhouse, run:

make
sudo make install

If your host has several PostgreSQL installations, you might need to specify the appropriate version of pg_config:

export PG_CONFIG=/usr/lib/postgresql/19/bin/pg_config
make
sudo make install

If curl-config is not in the path on you host, you can specify the path explicitly:

export CURL_CONFIG=/opt/homebrew/opt/curl/bin/curl-config
make
sudo make install

If you encounter an error such as:

"Makefile", line 8: Need an operator

You need to use GNU make, which may well be installed on your system as gmake:

gmake
gmake install
gmake installcheck

If you encounter an error such as:

make: pg_config: Command not found

Be sure that you have pg_config installed and in your path. If you used a package management system such as RPM to install PostgreSQL, be sure that the -devel package is also installed. If necessary tell the build process where to find it:

export PG_CONFIG=/path/to/pg_config
make
sudo make install

To install the extension in a custom prefix on PostgreSQL 18 or later, pass the prefix argument to install (but no other make targets):

sudo make install prefix=/usr/local/extras

Then ensure that the prefix is included in the following postgresql.conf parameters:

extension_control_path = '/usr/local/extras/postgresql/share:$system'
dynamic_library_path   = '/usr/local/extras/postgresql/lib:$libdir'

Testing

To run the test suite, once the extension has been installed, run

make installcheck

If you encounter an error such as:

ERROR:  must be owner of database regression

You need to run the test suite using a super user, such as the default "postgres" super user:

make installcheck PGUSER=postgres

Loading

Once pg_clickhouse is installed, you can add it to a database by connecting as a super user and running:

CREATE EXTENSION pg_clickhouse;

If you want to install pg_clickhouse and all of its supporting objects into a specific schema, use the SCHEMA clause to specify the schema, like so:

CREATE SCHEMA env;
CREATE EXTENSION pg_clickhouse SCHEMA env;

Dependencies

The pg_clickhouse extension requires PostgreSQL 13 or higher, libcurl, libuuid, liblz4, and libzstd. Building the extension requires a C compiler, libSSL, and GNU make.

Road Map

Our top focus is finishing pushdown coverage for analytic workloads before adding DML features. Our road map:

  • Get the remaining 10 un-pushed-down TPC-H queries optimally planned
  • Test and fix pushdown for the ClickBench queries
  • Support transparent pushdown of all PostgreSQL aggregate functions
  • Support transparent pushdown of all PostgreSQL functions
  • Allow server-level and session-level ClickHouse settings via CREATE SERVER and GUCs
  • Support all ClickHouse data types
  • Support lightweight DELETEs and UPDATEs
  • Support batch insertion via COPY
  • Add a function to execute an arbitrary ClickHouse query and return its results as a tables
  • Add support for pushdown of UNION queries when they all query the remote database

Authors

Copyright

  • Copyright (c) 2025-2026, ClickHouse
  • Portions Copyright (c) 2023-2025, Ildus Kurbangaliev
  • Portions Copyright (c) 2019-2023, Adjust GmbH
  • Portions Copyright (c) 2012-2019, PostgreSQL Global Development Group

About

Interfaces to query ClickHouse databases from PostgreSQL

Topics

Resources

License

Security policy

Stars

254 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages