PostgreSQL

The PostgreSQL engine allows to perform SELECT and INSERT queries on data that is stored on a remote PostgreSQL server.

Creating a Table

  1. CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
  2. (
  3. name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1] [TTL expr1],
  4. name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2] [TTL expr2],
  5. ...
  6. ) ENGINE = PostgreSQL('host:port', 'database', 'table', 'user', 'password'[, `schema`]);

See a detailed description of the CREATE TABLE query.

The table structure can differ from the original PostgreSQL table structure:

  • Column names should be the same as in the original PostgreSQL table, but you can use just some of these columns and in any order.
  • Column types may differ from those in the original PostgreSQL table. ClickHouse tries to cast values to the ClickHouse data types.
  • The external_table_functions_use_nulls setting defines how to handle Nullable columns. Default value: 1. If 0, the table function does not make Nullable columns and inserts default values instead of nulls. This is also applicable for NULL values inside arrays.

Engine Parameters

  • host:port — PostgreSQL server address.
  • database — Remote database name.
  • table — Remote table name.
  • user — PostgreSQL user.
  • password — User password.
  • schema — Non-default table schema. Optional.
  • on conflict ... — example: ON CONFLICT DO NOTHING. Optional. Note: adding this option will make insertion less efficient.

Implementation Details

SELECT queries on PostgreSQL side run as COPY (SELECT ...) TO STDOUT inside read-only PostgreSQL transaction with commit after each SELECT query.

Simple WHERE clauses such as =, !=, >, >=, <, <=, and IN are executed on the PostgreSQL server.

All joins, aggregations, sorting, IN [ array ] conditions and the LIMIT sampling constraint are executed in ClickHouse only after the query to PostgreSQL finishes.

INSERT queries on PostgreSQL side run as COPY "table_name" (field1, field2, ... fieldN) FROM STDIN inside PostgreSQL transaction with auto-commit after each INSERT statement.

PostgreSQL Array types are converted into ClickHouse arrays.

Note

Be careful - in PostgreSQL an array data, created like a type_name[], may contain multi-dimensional arrays of different dimensions in different table rows in same column. But in ClickHouse it is only allowed to have multidimensional arrays of the same count of dimensions in all table rows in same column.

Supports multiple replicas that must be listed by |. For example:

  1. CREATE TABLE test_replicas (id UInt32, name String) ENGINE = PostgreSQL(`postgres{2|3|4}:5432`, 'clickhouse', 'test_replicas', 'postgres', 'mysecretpassword');

Replicas priority for PostgreSQL dictionary source is supported. The bigger the number in map, the less the priority. The highest priority is 0.

In the example below replica example01-1 has the highest priority:

  1. <postgresql>
  2. <port>5432</port>
  3. <user>clickhouse</user>
  4. <password>qwerty</password>
  5. <replica>
  6. <host>example01-1</host>
  7. <priority>1</priority>
  8. </replica>
  9. <replica>
  10. <host>example01-2</host>
  11. <priority>2</priority>
  12. </replica>
  13. <db>db_name</db>
  14. <table>table_name</table>
  15. <where>id=10</where>
  16. <invalidate_query>SQL_QUERY</invalidate_query>
  17. </postgresql>
  18. </source>

Usage Example

Table in PostgreSQL:

  1. postgres=# CREATE TABLE "public"."test" (
  2. "int_id" SERIAL,
  3. "int_nullable" INT NULL DEFAULT NULL,
  4. "float" FLOAT NOT NULL,
  5. "str" VARCHAR(100) NOT NULL DEFAULT '',
  6. "float_nullable" FLOAT NULL DEFAULT NULL,
  7. PRIMARY KEY (int_id));
  8. CREATE TABLE
  9. postgres=# INSERT INTO test (int_id, str, "float") VALUES (1,'test',2);
  10. INSERT 0 1
  11. postgresql> SELECT * FROM test;
  12. int_id | int_nullable | float | str | float_nullable
  13. --------+--------------+-------+------+----------------
  14. 1 | | 2 | test |
  15. (1 row)

Table in ClickHouse, retrieving data from the PostgreSQL table created above:

  1. CREATE TABLE default.postgresql_table
  2. (
  3. `float_nullable` Nullable(Float32),
  4. `str` String,
  5. `int_id` Int32
  6. )
  7. ENGINE = PostgreSQL('localhost:5432', 'public', 'test', 'postges_user', 'postgres_password');
  1. SELECT * FROM postgresql_table WHERE str IN ('test');
  1. ┌─float_nullable─┬─str──┬─int_id─┐
  2. ᴺᵁᴸᴸ test 1
  3. └────────────────┴──────┴────────┘

Using Non-default Schema:

  1. postgres=# CREATE SCHEMA "nice.schema";
  2. postgres=# CREATE TABLE "nice.schema"."nice.table" (a integer);
  3. postgres=# INSERT INTO "nice.schema"."nice.table" SELECT i FROM generate_series(0, 99) as t(i)
  1. CREATE TABLE pg_table_schema_with_dots (a UInt32)
  2. ENGINE PostgreSQL('localhost:5432', 'clickhouse', 'nice.table', 'postgrsql_user', 'password', 'nice.schema');

See Also