2021-02-27 00:01:02 +00:00
---
toc_priority: 11
toc_title: PostgreSQL
---
2021-03-26 17:34:48 +00:00
# PostgreSQL {#postgresql}
2021-02-27 00:01:02 +00:00
The PostgreSQL engine allows to perform `SELECT` and `INSERT` queries on data that is stored on a remote PostgreSQL server.
## Creating a Table {#creating-a-table}
``` sql
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1] [TTL expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2] [TTL expr2],
...
2021-03-30 17:35:38 +00:00
) ENGINE = PostgreSQL('host:port', 'database', 'table', 'user', 'password'[, `schema` ]);
2021-02-27 00:01:02 +00:00
```
See a detailed description of the [CREATE TABLE ](../../../sql-reference/statements/create/table.md#create-table-query ) query.
2021-02-22 15:41:23 +00:00
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.
2021-06-27 11:07:24 +00:00
- Column types may differ from those in the original PostgreSQL table. ClickHouse tries to [cast ](../../../engines/database-engines/postgresql.md#data_types-support ) values to the ClickHouse data types.
2021-06-27 17:57:46 +00:00
- The [external_table_functions_use_nulls ](../../../operations/settings/settings.md#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.
2021-02-22 15:41:23 +00:00
2021-02-27 00:01:02 +00:00
**Engine Parameters**
2021-02-22 15:41:23 +00:00
- `host:port` — PostgreSQL server address.
2021-02-27 00:01:02 +00:00
- `database` — Remote database name.
- `table` — Remote table name.
2021-02-22 15:41:23 +00:00
- `user` — PostgreSQL user.
2021-02-27 00:01:02 +00:00
- `password` — User password.
2021-03-30 17:35:38 +00:00
- `schema` — Non-default table schema. Optional.
2021-08-24 12:29:42 +00:00
- `on conflict ...` — example: `ON CONFLICT DO NOTHING` . Optional. Note: adding this option will make insertion less efficient.
2021-03-30 17:35:38 +00:00
2021-12-25 13:37:44 +00:00
or via config (since version 21.11):
```
< named_collections >
< postgres1 >
< host > < / host >
< port > < / port >
< username > < / username >
< password > < / password >
< table > < / table >
< / postgres1 >
< postgres2 >
< host > < / host >
< port > < / port >
< username > < / username >
< password > < / password >
< / postgres2 >
< / named_collections >
```
Some parameters can be overriden by key value arguments:
``` sql
SELECT * FROM postgresql(postgres1, schema='schema1', table='table1');
```
2021-03-30 17:35:38 +00:00
## Implementation Details {#implementation-details}
2021-02-27 00:01:02 +00:00
2021-03-20 09:48:25 +00:00
`SELECT` queries on PostgreSQL side run as `COPY (SELECT ...) TO STDOUT` inside read-only PostgreSQL transaction with commit after each `SELECT` query.
2021-02-27 00:01:02 +00:00
2021-03-30 17:35:38 +00:00
Simple `WHERE` clauses such as `=` , `!=` , `>` , `>=` , `<` , `<=` , and `IN` are executed on the PostgreSQL server.
2021-02-27 00:01:02 +00:00
2021-02-22 15:41:23 +00:00
All joins, aggregations, sorting, `IN [ array ]` conditions and the `LIMIT` sampling constraint are executed in ClickHouse only after the query to PostgreSQL finishes.
2021-02-27 00:01:02 +00:00
2021-03-20 09:48:25 +00:00
`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.
2021-02-22 15:41:23 +00:00
2021-03-30 17:35:38 +00:00
PostgreSQL `Array` types are converted into ClickHouse arrays.
2021-03-20 09:48:25 +00:00
!!! info "Note"
2021-03-30 17:35:38 +00:00
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.
2021-07-29 15:27:50 +00:00
2021-06-27 09:26:03 +00:00
Supports multiple replicas that must be listed by `|` . For example:
2021-06-24 19:29:57 +00:00
```sql
CREATE TABLE test_replicas (id UInt32, name String) ENGINE = PostgreSQL(`postgres{2|3|4}:5432`, 'clickhouse', 'test_replicas', 'postgres', 'mysecretpassword');
```
2021-03-30 17:35:38 +00:00
2021-07-29 15:20:55 +00:00
Replicas priority for PostgreSQL dictionary source is supported. The bigger the number in map, the less the priority. The highest priority is `0` .
2021-03-31 22:48:48 +00:00
In the example below replica `example01-1` has the highest priority:
```xml
< postgresql >
< port > 5432< / port >
< user > clickhouse< / user >
< password > qwerty< / password >
< replica >
< host > example01-1< / host >
< priority > 1< / priority >
< / replica >
< replica >
< host > example01-2< / host >
< priority > 2< / priority >
< / replica >
< db > db_name< / db >
< table > table_name< / table >
< where > id=10< / where >
< invalidate_query > SQL_QUERY< / invalidate_query >
< / postgresql >
< / source >
```
2021-02-22 15:41:23 +00:00
## Usage Example {#usage-example}
Table in PostgreSQL:
2021-02-27 00:01:02 +00:00
``` text
2021-02-22 15:41:23 +00:00
postgres=# CREATE TABLE "public"."test" (
"int_id" SERIAL,
"int_nullable" INT NULL DEFAULT NULL,
"float" FLOAT NOT NULL,
"str" VARCHAR(100) NOT NULL DEFAULT '',
"float_nullable" FLOAT NULL DEFAULT NULL,
PRIMARY KEY (int_id));
CREATE TABLE
2021-03-30 17:35:38 +00:00
postgres=# INSERT INTO test (int_id, str, "float") VALUES (1,'test',2);
2021-02-22 15:41:23 +00:00
INSERT 0 1
2021-03-30 17:35:38 +00:00
postgresql> SELECT * FROM test;
2021-04-08 19:32:27 +00:00
int_id | int_nullable | float | str | float_nullable
--------+--------------+-------+------+----------------
1 | | 2 | test |
(1 row)
2021-02-27 00:01:02 +00:00
```
2021-02-22 15:41:23 +00:00
Table in ClickHouse, retrieving data from the PostgreSQL table created above:
``` sql
CREATE TABLE default.postgresql_table
(
`float_nullable` Nullable(Float32),
`str` String,
`int_id` Int32
)
ENGINE = PostgreSQL('localhost:5432', 'public', 'test', 'postges_user', 'postgres_password');
```
2021-02-27 00:01:02 +00:00
``` sql
2021-03-30 17:35:38 +00:00
SELECT * FROM postgresql_table WHERE str IN ('test');
2021-02-27 00:01:02 +00:00
```
``` text
2021-02-22 15:41:23 +00:00
┌─float_nullable─┬─str──┬─int_id─┐
│ ᴺᵁᴸᴸ │ test │ 1 │
└────────────────┴──────┴────────┘
2021-03-30 17:35:38 +00:00
```
Using Non-default Schema:
```text
postgres=# CREATE SCHEMA "nice.schema";
postgres=# CREATE TABLE "nice.schema"."nice.table" (a integer);
postgres=# INSERT INTO "nice.schema"."nice.table" SELECT i FROM generate_series(0, 99) as t(i)
```
```sql
CREATE TABLE pg_table_schema_with_dots (a UInt32)
ENGINE PostgreSQL('localhost:5432', 'clickhouse', 'nice.table', 'postgrsql_user', 'password', 'nice.schema');
2021-02-27 00:01:02 +00:00
```
2021-03-20 09:48:25 +00:00
**See Also**
2021-02-27 00:01:02 +00:00
2021-03-20 09:48:25 +00:00
- [The `postgresql` table function ](../../../sql-reference/table-functions/postgresql.md )
2021-02-27 00:29:56 +00:00
- [Using PostgreSQL as a source of external dictionary ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-sources.md#dicts-external_dicts_dict_sources-postgresql )
2021-02-27 00:01:02 +00:00
2021-09-19 20:05:54 +00:00
[Original article ](https://clickhouse.com/docs/en/engines/table-engines/integrations/postgresql/ ) <!--hide-->