Documentation Index
Fetch the complete documentation index at: https://private-7c7dfe99-page-updates.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
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 the
PostgreSQLtable engine, for reading from a PostgreSQL table - using the experimental
MaterializedPostgreSQLdatabase engine, for syncing a database in PostgreSQL with a database in ClickHouse
Using the PostgreSQL table engine
ThePostgreSQL 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.
- Setting up PostgreSQL
- In
postgresql.conf, add the following entry to enable PostgreSQL to listen on the network interfaces:
- Create a user to connect from ClickHouse. For demonstration purposes, this example grants full superuser rights.
- Create a new database in PostgreSQL:
- Create a new table:
- Let’s add a few rows for testing:
- To configure PostgreSQL to allow connections to the new database with the new user for replication, add the following entry to the
pg_hba.conffile. Update the address line with either the subnet or IP address of your PostgreSQL server:
- Reload the
pg_hba.confconfiguration (adjust this command depending on your version):
- Verify the new
clickhouse_usercan login:
If you’re 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.
- Define a Table in ClickHouse
- Login to the
clickhouse-client:
- Let’s create a new database:
- Create a table that uses the
PostgreSQL:
| parameter | Description | example |
|---|---|---|
| host:port | hostname or IP and port | postgres-host.domain.com:5432 |
| database | PostgreSQL database name | db_in_psg |
| user | username to connect to postgres | clickhouse_user |
| password | password to connect to postgres | ClickHouse_123 |
View the PostgreSQL table engine doc page for a complete list of parameters.
3 Test the Integration
- In ClickHouse, view initial rows:
- Back in PostgreSQL, add a couple of rows to the table:
- Those two new rows should appear in your ClickHouse table:
- Let’s see what happens when you add rows to the ClickHouse table:
- The rows added in ClickHouse should appear in the table in PostgreSQL:
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
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
- In PostgreSQL
- In
postgresql.conf, set minimum listen levels, replication wal level and replication slots:
logical wal level and minimum 2 replication slots
- Using an admin account, create a user to connect from ClickHouse:
- create a new database:
- connect to the new database in
psql:
- create a new table:
- add initial rows:
- 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.conffile:
- reload the
pg_hba.confconfiguration with something like this (adjust for your version):
- Test the login with new
clickhouse_user:
- In ClickHouse
- log into the ClickHouse CLI
- Enable the PostgreSQL experimental feature for the database engine:
- Create the new database to be replicated and define the initial table:
| parameter | Description | example |
|---|---|---|
| host:port | hostname or IP and port | postgres-host.domain.com:5432 |
| database | PostgreSQL database name | db1 |
| user | username to connect to postgres | clickhouse_user |
| password | password to connect to postgres | ClickHouse_123 |
| settings | additional settings for the engine | materialized_postgresql_tables_list = ‘table1’ |
For complete guide to the PostgreSQL database engine, refer to https://clickhouse.com/docs/engines/database-engines/materialized-postgresql/#settings
- Verify the initial table has data:
- Test basic replication
- In PostgreSQL, add new rows:
- In ClickHouse, verify the new rows are visible:
- 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 aren’t supported for this replication, the engine can be set to detect changes and reload the tables when there are structural changes made.
For more features available for advanced options, please see the reference documentation.