diff --git a/docs/en/engines/table-engines/integrations/postgresql.md b/docs/en/engines/table-engines/integrations/postgresql.md index 2fcc0b10e78..378b09e8259 100644 --- a/docs/en/engines/table-engines/integrations/postgresql.md +++ b/docs/en/engines/table-engines/integrations/postgresql.md @@ -111,7 +111,7 @@ In the example below replica `example01-1` has the highest priority: ## Usage Example {#usage-example} -Table in PostgreSQL: +### Table in PostgreSQL ``` text postgres=# CREATE TABLE "public"."test" ( @@ -134,7 +134,9 @@ postgresql> SELECT * FROM test; (1 row) ``` -Table in ClickHouse, retrieving data from the PostgreSQL table created above: +### Creating Table in ClickHouse, and connecting to PostgreSQL table created above + +This example uses the [PostgreSQL table engine](/docs/en/engines/table-engines/integrations/postgresql) to connect the ClickHouse table to the PostgreSQL table: ``` sql CREATE TABLE default.postgresql_table @@ -146,6 +148,35 @@ CREATE TABLE default.postgresql_table ENGINE = PostgreSQL('localhost:5432', 'public', 'test', 'postges_user', 'postgres_password'); ``` +### Inserting initial data from PostgreSQL table into ClickHouse table, using a SELECT query + +The [postgresql table function](/docs/en/sql-reference/table-functions/postgresql) copies the data from PostgreSQL to ClickHouse, which is often used for improving the query performance of the data by querying or performing analytics in ClickHouse rather than in PostgreSQL, or can also be used for migrating data from PostgreSQL to ClickHouse: + +``` sql +INSERT INTO default.postgresql_table +SELECT * FROM postgresql('localhost:5432', 'public', 'test', 'postges_user', 'postgres_password'); +``` + +### Inserting incremental data from PostgreSQL table into ClickHouse table + +If then performing ongoing synchronization between the PostgreSQL table and ClickHouse table after the initial insert, you can use a WHERE clause in ClickHouse to insert only data added to PostgreSQL based on a timestamp or unique sequence ID. + +This would require keeping track of the max ID or timestamp previously added, such as the following: + +``` sql +SELECT max(`int_id`) AS maxIntID FROM default.postgresql_table; +``` + +Then inserting values from PostgreSQL table greater than the max + +``` sql +INSERT INTO default.postgresql_table +SELECT * FROM postgresql('localhost:5432', 'public', 'test', 'postges_user', 'postgres_password'); +WHERE int_id > maxIntID; +``` + +### Selecting data from the resulting ClickHouse table + ``` sql SELECT * FROM postgresql_table WHERE str IN ('test'); ``` @@ -156,7 +187,7 @@ SELECT * FROM postgresql_table WHERE str IN ('test'); └────────────────┴──────┴────────┘ ``` -Using Non-default Schema: +### Using Non-default Schema ```text postgres=# CREATE SCHEMA "nice.schema";