Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update postgresql.md #48909

Merged
merged 3 commits into from
Apr 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 34 additions & 3 deletions docs/en/engines/table-engines/integrations/postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" (
Expand All @@ -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.md) to connect the ClickHouse table to the PostgreSQL table:

``` sql
CREATE TABLE default.postgresql_table
Expand All @@ -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.md) 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');
```
Expand All @@ -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";
Expand Down