ClickHouse/docs/en/sql-reference/table-functions/postgresql.md
Olga Revyakina 106772af33 Fixes
2021-02-27 03:59:03 +03:00

2.1 KiB

toc_priority toc_title
54 postgresql

postgresql

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

Syntax

postgresql('host:port', 'database', 'table', 'user', 'password')

Arguments

  • host:port — PostgreSQL server address.
  • database — Remote database name.
  • table — Remote table name.
  • user — PostgreSQL user.
  • password — User password.

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.

Examples

Consider the table in PostgreSQL:

postgre> CREATE TABLE IF NOT EXISTS test_table (a integer, b text, c integer)
postgre> INSERT INTO test_table (a, b, c) VALUES (1, 2, 3), (4, 5, 6)

Selecting data from ClickHouse:

SELECT * FROM postgresql('localhost:5432', 'test_database', 'test_table', 'postgres', 'mysecretpassword');
┌─a─┬─b─┬─c─┐
│ 1 │ 2 │ 3 │
│ 4 │ 5 │ 6 │
└───┴───┴───┘

Inserting into PostgreSQL from ClickHouse:

INSERT INTO FUNCTION postgresql('localhost:5432', 'test_database', 'test_table', 'postgres', 'mysecretpassword') (a, b, c) VALUES (7, 8, 9);
SELECT * FROM postgresql('localhost:5432', 'test_database', 'test_table', 'postgres', 'mysecretpassword');
┌─a─┬─b─┬─c─┐
│ 1 │ 2 │ 3 │
│ 4 │ 5 │ 6 │
│ 7 │ 8 │ 9 │
└───┴───┴───┘

See Also

Original article