postgresql

Allows SELECT and INSERT queries to be performed on data that is stored on a remote PostgreSQL server.

Syntax

  1. postgresql('host:port', 'database', 'table', 'user', 'password'[, `schema`])

Arguments

  • 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.

Returned Value

A table object with the same columns as the original PostgreSQL table.

Note

In the INSERT query to distinguish table function postgresql(...) from table name with column names list you must use keywords FUNCTION or TABLE FUNCTION. See examples below.

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 converts into ClickHouse arrays.

Note

Be careful, in PostgreSQL an array data type column like Integer[] may contain arrays of different dimensions in different rows, but in ClickHouse it is only allowed to have multidimensional arrays of the same dimension in all rows.

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

  1. SELECT name FROM postgresql(`postgres{1|2|3}:5432`, 'postgres_database', 'postgres_table', 'user', 'password');

or

  1. SELECT name FROM postgresql(`postgres1:5431|postgres2:5432`, 'postgres_database', 'postgres_table', 'user', 'password');

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

Examples

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)

Selecting data from ClickHouse:

  1. SELECT * FROM postgresql('localhost:5432', 'test', 'test', 'postgresql_user', 'password') WHERE str IN ('test');
  1. ┌─int_id─┬─int_nullable─┬─float─┬─str──┬─float_nullable─┐
  2. 1 ᴺᵁᴸᴸ 2 test ᴺᵁᴸᴸ
  3. └────────┴──────────────┴───────┴──────┴────────────────┘

Inserting:

  1. INSERT INTO TABLE FUNCTION postgresql('localhost:5432', 'test', 'test', 'postgrsql_user', 'password') (int_id, float) VALUES (2, 3);
  2. SELECT * FROM postgresql('localhost:5432', 'test', 'test', 'postgresql_user', 'password');
  1. ┌─int_id─┬─int_nullable─┬─float─┬─str──┬─float_nullable─┐
  2. 1 ᴺᵁᴸᴸ 2 test ᴺᵁᴸᴸ
  3. 2 ᴺᵁᴸᴸ 3 ᴺᵁᴸᴸ
  4. └────────┴──────────────┴───────┴──────┴────────────────┘

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