Skip to content
pg_clickhouse

pg_clickhouse

pg_clickhouse : Interfaces to query ClickHouse databases from PostgreSQL

Overview

ID Extension Package Version Category License Language
2460
pg_clickhouse
pg_clickhouse
0.3.2
OLAP
Apache-2.0
C++
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--sLd-r
No
Yes
Yes
Yes
yes
no
Relationships
See Also
pg_duckdb
duckdb_fdw
citus
columnar
citus_columnar
clickhouse_fdw
postgres_fdw
dblink

release 0.3.1; SQL v0.3

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT
PIGSTY
0.3.2
18
17
16
15
14
pg_clickhouse -
RPM
PIGSTY
0.3.1
18
17
16
15
14
pg_clickhouse_$v -
DEB
PIGSTY
0.3.2
18
17
16
15
14
postgresql-$v-clickhouse -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
el8.aarch64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
el9.x86_64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
el9.aarch64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
el10.x86_64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
el10.aarch64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
d12.x86_64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
d12.aarch64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
d13.x86_64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
d13.aarch64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
u22.x86_64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
u22.aarch64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
u24.x86_64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
u24.aarch64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
u26.x86_64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
u26.aarch64
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
PIGSTY 0.3.2
Package Version OS ORG SIZE File URL
pg_clickhouse_18 0.3.2 el8.x86_64 pigsty 147.6 KiB pg_clickhouse_18-0.3.2-1PIGSTY.el8.x86_64.rpm
pg_clickhouse_18 0.3.1 el8.x86_64 pigsty 143.9 KiB pg_clickhouse_18-0.3.1-1PIGSTY.el8.x86_64.rpm
pg_clickhouse_18 0.3.2 el8.aarch64 pigsty 146.6 KiB pg_clickhouse_18-0.3.2-1PIGSTY.el8.aarch64.rpm
pg_clickhouse_18 0.3.1 el8.aarch64 pigsty 142.9 KiB pg_clickhouse_18-0.3.1-1PIGSTY.el8.aarch64.rpm
pg_clickhouse_18 0.3.2 el9.x86_64 pigsty 143.8 KiB pg_clickhouse_18-0.3.2-1PIGSTY.el9.x86_64.rpm
pg_clickhouse_18 0.3.1 el9.x86_64 pigsty 134.9 KiB pg_clickhouse_18-0.3.1-1PIGSTY.el9.x86_64.rpm
pg_clickhouse_18 0.3.2 el9.aarch64 pigsty 142.0 KiB pg_clickhouse_18-0.3.2-1PIGSTY.el9.aarch64.rpm
pg_clickhouse_18 0.3.1 el9.aarch64 pigsty 138.6 KiB pg_clickhouse_18-0.3.1-1PIGSTY.el9.aarch64.rpm
pg_clickhouse_18 0.3.2 el10.x86_64 pigsty 141.1 KiB pg_clickhouse_18-0.3.2-1PIGSTY.el10.x86_64.rpm
pg_clickhouse_18 0.3.1 el10.x86_64 pigsty 136.6 KiB pg_clickhouse_18-0.3.1-1PIGSTY.el10.x86_64.rpm
pg_clickhouse_18 0.3.2 el10.aarch64 pigsty 144.7 KiB pg_clickhouse_18-0.3.2-1PIGSTY.el10.aarch64.rpm
pg_clickhouse_18 0.3.1 el10.aarch64 pigsty 141.1 KiB pg_clickhouse_18-0.3.1-1PIGSTY.el10.aarch64.rpm
postgresql-18-clickhouse 0.3.2 d12.x86_64 pigsty 392.4 KiB postgresql-18-clickhouse_0.3.2-1PIGSTY~bookworm_amd64.deb
postgresql-18-clickhouse 0.3.2 d12.aarch64 pigsty 387.6 KiB postgresql-18-clickhouse_0.3.2-1PIGSTY~bookworm_arm64.deb
postgresql-18-clickhouse 0.3.2 d13.x86_64 pigsty 392.8 KiB postgresql-18-clickhouse_0.3.2-1PIGSTY~trixie_amd64.deb
postgresql-18-clickhouse 0.3.2 d13.aarch64 pigsty 389.1 KiB postgresql-18-clickhouse_0.3.2-1PIGSTY~trixie_arm64.deb
postgresql-18-clickhouse 0.3.2 u22.x86_64 pigsty 415.1 KiB postgresql-18-clickhouse_0.3.2-1PIGSTY~jammy_amd64.deb
postgresql-18-clickhouse 0.3.2 u22.aarch64 pigsty 417.3 KiB postgresql-18-clickhouse_0.3.2-1PIGSTY~jammy_arm64.deb
postgresql-18-clickhouse 0.3.2 u24.x86_64 pigsty 396.5 KiB postgresql-18-clickhouse_0.3.2-1PIGSTY~noble_amd64.deb
postgresql-18-clickhouse 0.3.2 u24.aarch64 pigsty 399.0 KiB postgresql-18-clickhouse_0.3.2-1PIGSTY~noble_arm64.deb
postgresql-18-clickhouse 0.3.2 u26.x86_64 pigsty 392.8 KiB postgresql-18-clickhouse_0.3.2-1PIGSTY~resolute_amd64.deb
postgresql-18-clickhouse 0.3.2 u26.aarch64 pigsty 394.9 KiB postgresql-18-clickhouse_0.3.2-1PIGSTY~resolute_arm64.deb

Source

pig build pkg pg_clickhouse;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pg_clickhouse;		# install via package name, for the active PG version

pig install pg_clickhouse -v 18;   # install for PG 18
pig install pg_clickhouse -v 17;   # install for PG 17
pig install pg_clickhouse -v 16;   # install for PG 16
pig install pg_clickhouse -v 15;   # install for PG 15
pig install pg_clickhouse -v 14;   # install for PG 14

Config this extension to shared_preload_libraries:

shared_preload_libraries = 'pg_clickhouse';

Create this extension with:

CREATE EXTENSION pg_clickhouse;

Usage

Sources: README, reference, tutorial, v0.3.1 release notes, changelog

pg_clickhouse runs analytics queries on ClickHouse from PostgreSQL through the clickhouse_fdw foreign data wrapper. Upstream documents PostgreSQL 13+ and ClickHouse 23+ support; Pigsty packages version 0.3.1 for PostgreSQL 14-18.

Connect PostgreSQL to ClickHouse

CREATE EXTENSION pg_clickhouse;

CREATE SERVER taxi_srv
FOREIGN DATA WRAPPER clickhouse_fdw
OPTIONS (driver 'binary', host 'localhost', dbname 'taxi');

CREATE USER MAPPING FOR CURRENT_USER
SERVER taxi_srv
OPTIONS (user 'default');

CREATE SCHEMA taxi;
IMPORT FOREIGN SCHEMA taxi FROM SERVER taxi_srv INTO taxi;

Server options documented upstream:

  • driver: required, binary or http
  • host
  • port
  • dbname
  • fetch_size: HTTP streaming batch size; 0 disables streaming

User mapping options:

  • user
  • password

Common operations

ALTER EXTENSION pg_clickhouse UPDATE;
ALTER EXTENSION pg_clickhouse UPDATE TO '0.3';
SELECT pgch_version();
DROP SERVER taxi_srv CASCADE;

IMPORT FOREIGN SCHEMA also supports LIMIT TO (...) and EXCEPT (...). The reference warns that imported mixed-case identifiers are double-quoted in PostgreSQL and must be queried with quotes.

Query and write notes

SELECT, EXPLAIN, prepared statements, INSERT, and COPY can operate on pg_clickhouse foreign tables. Use EXPLAIN (VERBOSE) to inspect the remote SQL that will be sent to ClickHouse.

EXPLAIN (VERBOSE)
SELECT node_id, count(*)
FROM logs
GROUP BY node_id;

INSERT INTO nodes(node_id, name, region, arch, os)
VALUES (9, 'west-node', 'us-west-2', 'amd64', 'Linux');

COPY into a foreign table is documented, but upstream notes that it currently uses INSERT statements because FDW batch insertion is still future work.

Version and pushdown notes

  • The reference documents separate library and extension versions; pgch_version() reports the loaded library version.
  • Release v0.3.1 is binary-only for existing SQL version 0.3; it does not require ALTER EXTENSION UPDATE when upgrading from v0.3.0.
  • Release v0.3.1 replaces the client library with ClickHouse/clickhouse-c, streams result blocks, and adds rectangular multidimensional array support for SELECT and INSERT.
  • Release v0.3.1 also adds pushdown for pg_re2 0.3.0 functions such as re2extractallgroupshorizontal, re2extractallgroupsvertical, re2regexpquotemeta, and re2splitbyregexp, and fixes UInt16 casts to PostgreSQL int4.
  • Release v0.3.0 uses SQL version 0.3; run ALTER EXTENSION pg_clickhouse UPDATE TO '0.3' to apply its SQL-level privilege change.
  • Release v0.3.0 adds pushdown for re2 functions, soundex(), two-argument levenshtein(), compatible to_char(timestamp[tz], fmt), selected builtin functions, and JSON/JSONB path operations.
  • ClickHouse JSON maps to PostgreSQL jsonb or json; the binary driver’s JSON mapping requires ClickHouse 24.10 or later.
  • pg_clickhouse.pushdown_regex controls built-in PostgreSQL regex pushdown. Upstream recommends considering the re2 extension for regex work that should push down directly.

Caveats

  • In 0.3.0, clickhouse_raw_query(text, text) is no longer executable by PUBLIC; grant it only to roles that need ad-hoc ClickHouse queries.
  • This is positioned upstream as an analytics-first extension; lightweight DELETE and UPDATE support remain on the roadmap.
  • For full examples, follow the official tutorial, which creates a ClickHouse taxi database, imports it through IMPORT FOREIGN SCHEMA, and queries the resulting foreign tables.
Last updated on