Skip to main content
Skip to main content

PostgreSQL

A full migration guide for PostgreSQL to ClickHouse, including advice on data modeling and equivalent concepts, can be found here. The following describes how to connect ClickHouse and PostgreSQL.

Connecting ClickHouse to PostgreSQL

This page covers following options for integrating PostgreSQL with ClickHouse:

  • using ClickPipes, the managed integration service for ClickHouse Cloud - now in Private Preview. Please sign up here
  • using PeerDB by ClickHouse, a CDC tool specifically designed for PostgreSQL database replication to both self-hosted ClickHouse and ClickHouse Cloud
    • PeerDB is now available natively in ClickHouse Cloud - Blazing-fast Postgres to ClickHouse CDC with our new ClickPipe connector - now in Private Preview. Please sign up here
  • using the PostgreSQL table engine, for reading from a PostgreSQL table
  • using the experimental MaterializedPostgreSQL database engine, for syncing a database in PostgreSQL with a database in ClickHouse

Using ClickPipes (powered by PeerDB)

PeerDB is now available natively in ClickHouse Cloud - Blazing-fast Postgres to ClickHouse CDC with our new ClickPipe connector - now in Private Preview. Please sign up here

Using the PostgreSQL Table Engine

The PostgreSQL table engine allows SELECT and INSERT operations on data stored on the remote PostgreSQL server from ClickHouse. This article is to illustrate basic methods of integration using one table.

1. Setting up PostgreSQL

  1. In postgresql.conf, add the following entry to enable PostgreSQL to listen on the network interfaces:
  1. Create a user to connect from ClickHouse. For demonstration purposes, this example grants full superuser rights.
  1. Create a new database in PostgreSQL:
  1. Create a new table:
  1. Let's add a few rows for testing:
  1. To configure PostgreSQL to allow connections to the new database with the new user for replication, add the following entry to the pg_hba.conf file. Update the address line with either the subnet or IP address of your PostgreSQL server:
  1. Reload the pg_hba.conf configuration (adjust this command depending on your version):
  1. Verify the new clickhouse_user can login:
Note

If you are using this feature in ClickHouse Cloud, you may need the to allow the ClickHouse Cloud IP addresses to access your PostgreSQL instance. Check the ClickHouse Cloud Endpoints API for egress traffic details.

2. Define a Table in ClickHouse

  1. Login to the clickhouse-client:
  1. Let's create a new database:
  1. Create a table that uses the PostgreSQL:

The minimum parameters needed are:

parameterDescriptionexample
host:porthostname or IP and portpostgres-host.domain.com:5432
databasePostgreSQL database namedb_in_psg
userusername to connect to postgresclickhouse_user
passwordpassword to connect to postgresClickHouse_123
Note

View the PostgreSQL table engine doc page for a complete list of parameters.

3 Test the Integration

  1. In ClickHouse, view initial rows:

The ClickHouse table should automatically be populated with the two rows that already existed in the table in PostgreSQL:

  1. Back in PostgreSQL, add a couple of rows to the table:
  1. Those two new rows should appear in your ClickHouse table:

The response should be:

  1. Let's see what happens when you add rows to the ClickHouse table:
  1. The rows added in ClickHouse should appear in the table in PostgreSQL:

This example demonstrated the basic integration between PostgreSQL and ClickHouse using the PostrgeSQL table engine. Check out the doc page for the PostgreSQL table engine for more features, such as specifying schemas, returning only a subset of columns, and connecting to multiple replicas. Also check out the ClickHouse and PostgreSQL - a match made in data heaven - part 1 blog.

Using the MaterializedPostgreSQL database engine

Not supported in ClickHouse Cloud
Experimental feature. Learn more.

The PostgreSQL database engine uses the PostgreSQL replication features to create a replica of the database with all or a subset of schemas and tables. This article is to illustrate basic methods of integration using one database, one schema and one table.

In the following procedures, the PostgreSQL CLI (psql) and the ClickHouse CLI (clickhouse-client) are used. The PostgreSQL server is installed on linux. The following has minimum settings if the postgresql database is new test install

1. In PostgreSQL

  1. In postgresql.conf, set minimum listen levels, replication wal level and replication slots:

add the following entries:

*ClickHouse needs minimum of logical wal level and minimum 2 replication slots

  1. Using an admin account, create a user to connect from ClickHouse:

*for demonstration purposes, full superuser rights have been granted.

  1. create a new database:
  1. connect to the new database in psql:
  1. create a new table:
  1. add initial rows:
  1. Configure PostgreSQL allow connections to the new database with the new user for replication. Below is the minimum entry to add to the pg_hba.conf file:

*for demonstration purposes, this is using clear text password authentication method. update the address line with either the subnet or the address of the server per PostgreSQL documentation

  1. reload the pg_hba.conf configuration with something like this (adjust for your version):
  1. Test the login with new clickhouse_user:

2. In ClickHouse

  1. log into the ClickHouse CLI
  1. Enable the PostgreSQL experimental feature for the database engine:
  1. Create the new database to be replicated and define the initial table:

minimum options:

parameterDescriptionexample
host:porthostname or IP and portpostgres-host.domain.com:5432
databasePostgreSQL database namedb1
userusername to connect to postgresclickhouse_user
passwordpassword to connect to postgresClickHouse_123
settingsadditional settings for the enginematerialized_postgresql_tables_list = 'table1'
Info

For complete guide to the PostgreSQL database engine, refer to https://clickhouse.com/docs/en/engines/database-engines/materialized-postgresql/#settings

  1. Verify the initial table has data:

3. Test basic replication

  1. In PostgreSQL, add new rows:
  1. In ClickHouse, verify the new rows are visible:

4. Summary

This integration guide focused on a simple example on how to replicate a database with a table, however, there exist more advanced options which include replicating the whole database or adding new tables and schemas to the existing replications. Although DDL commands are not supported for this replication, the engine can be set to detect changes and reload the tables when there are structural changes made.

Info

For more features available for advanced options, please see the reference documentation.