2021-02-27 00:01:02 +00:00
---
2021-02-22 15:41:23 +00:00
toc_priority: 42
2021-02-27 00:01:02 +00:00
toc_title: postgresql
---
# postgresql {#postgresql}
Allows `SELECT` and `INSERT` queries to be performed on data that is stored on a remote PostgreSQL server.
**Syntax**
``` sql
2021-03-30 17:35:38 +00:00
postgresql('host:port', 'database', 'table', 'user', 'password'[, `schema` ])
2021-02-27 00:01:02 +00:00
```
**Arguments**
- `host:port` — PostgreSQL server address.
- `database` — Remote database name.
- `table` — Remote table name.
- `user` — PostgreSQL user.
- `password` — User password.
2021-03-30 17:35:38 +00:00
- `schema` — Non-default table schema. Optional.
2021-02-27 00:01:02 +00:00
2021-03-30 17:35:38 +00:00
**Returned Value**
A table object with the same columns as the original PostgreSQL table.
!!! info "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 {#implementation-details}
2021-02-22 15:41:23 +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-22 15:41:23 +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-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-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
PostgreSQL Array types converts into ClickHouse arrays.
2021-02-23 08:08:11 +00:00
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 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.
2021-02-22 15:41:23 +00:00
2021-03-30 17:35:38 +00:00
Supports replicas priority for PostgreSQL dictionary source. The bigger the number in map, the less the priority. The highest priority is `0` .
2021-02-27 00:01:02 +00:00
**Examples**
2021-02-22 15:41:23 +00:00
Table in PostgreSQL:
2021-02-27 00:01:02 +00:00
2021-02-22 15:41:23 +00:00
``` text
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 |
2021-02-22 15:41:23 +00:00
(1 row)
2021-02-27 00:01:02 +00:00
```
Selecting data from ClickHouse:
2021-02-22 15:41:23 +00:00
```sql
2021-02-23 08:08:11 +00:00
SELECT * FROM postgresql('localhost:5432', 'test', 'test', 'postgresql_user', 'password') WHERE str IN ('test');
2021-02-27 00:01:02 +00:00
```
``` text
2021-02-22 15:41:23 +00:00
┌─int_id─┬─int_nullable─┬─float─┬─str──┬─float_nullable─┐
│ 1 │ ᴺᵁᴸᴸ │ 2 │ test │ ᴺᵁᴸᴸ │
└────────┴──────────────┴───────┴──────┴────────────────┘
2021-02-27 00:01:02 +00:00
```
2021-02-22 15:41:23 +00:00
Inserting:
2021-02-27 00:01:02 +00:00
```sql
2021-02-22 15:41:23 +00:00
INSERT INTO TABLE FUNCTION postgresql('localhost:5432', 'test', 'test', 'postgrsql_user', 'password') (int_id, float) VALUES (2, 3);
SELECT * FROM postgresql('localhost:5432', 'test', 'test', 'postgresql_user', 'password');
2021-02-27 00:01:02 +00:00
```
``` text
2021-02-22 15:41:23 +00:00
┌─int_id─┬─int_nullable─┬─float─┬─str──┬─float_nullable─┐
│ 1 │ ᴺᵁᴸᴸ │ 2 │ test │ ᴺᵁᴸᴸ │
│ 2 │ ᴺᵁᴸᴸ │ 3 │ │ ᴺᵁᴸᴸ │
└────────┴──────────────┴───────┴──────┴────────────────┘
2021-02-27 00:01:02 +00:00
```
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
**See Also**
2021-02-27 00:59:03 +00:00
- [The PostgreSQL table engine ](../../engines/table-engines/integrations/postgresql.md )
- [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-03-09 20:46:47 +00:00
[Original article ](https://clickhouse.tech/docs/en/sql-reference/table-functions/postgresql/ ) <!--hide-->