> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 원격 PostgreSQL 서버의 데이터베이스에 연결할 수 있습니다.

# PostgreSQL

원격 [PostgreSQL](https://www.postgresql.org) 서버의 데이터베이스에 연결할 수 있습니다. ClickHouse와 PostgreSQL 간에 데이터를 주고받기 위한 읽기 및 쓰기 작업(`SELECT` 및 `INSERT` 쿼리)을 지원합니다.

`SHOW TABLES` 및 `DESCRIBE TABLE` 쿼리를 통해 원격 PostgreSQL의 테이블 목록과 테이블 구조에 실시간으로 접근할 수 있습니다.

테이블 구조 수정(`ALTER TABLE ... ADD|DROP COLUMN`)을 지원합니다. `use_table_cache` 매개변수(아래 엔진 매개변수 참조)가 `1`로 설정되면 테이블 구조가 캐시되어 변경 여부를 확인하지 않지만, `DETACH` 및 `ATTACH` 쿼리로 업데이트할 수 있습니다.

<div id="creating-a-database">
  ## 데이터베이스 생성하기
</div>

```sql theme={null}
CREATE DATABASE test_database
ENGINE = PostgreSQL('host:port', 'database', 'user', 'password'[, `schema`, `use_table_cache`]);
```

**엔진 매개변수**

* `host:port` — PostgreSQL 서버 주소입니다.
* `database` — 원격 데이터베이스 이름입니다.
* `user` — PostgreSQL 사용자입니다.
* `password` — 사용자 비밀번호입니다.
* `schema` — PostgreSQL 스키마입니다.
* `use_table_cache` — 데이터베이스 테이블 구조를 캐시할지 여부를 정의합니다. 선택 사항입니다. 기본값: `0`입니다.

<div id="data_types-support">
  ## 데이터 타입 지원
</div>

| PostgreSQL       | ClickHouse                                           |
| ---------------- | ---------------------------------------------------- |
| DATE             | [Date](/ko/reference/data-types/date)                |
| TIMESTAMP        | [DateTime](/ko/reference/data-types/datetime)        |
| REAL             | [Float32](/ko/reference/data-types/float)            |
| DOUBLE           | [Float64](/ko/reference/data-types/float)            |
| DECIMAL, NUMERIC | [Decimal](/ko/reference/data-types/decimal)          |
| SMALLINT         | [Int16](/ko/reference/data-types/int-uint)           |
| INTEGER          | [Int32](/ko/reference/data-types/int-uint)           |
| BIGINT           | [Int64](/ko/reference/data-types/int-uint)           |
| SERIAL           | [UInt32](/ko/reference/data-types/int-uint)          |
| BIGSERIAL        | [UInt64](/ko/reference/data-types/int-uint)          |
| TEXT, CHAR       | [String](/ko/reference/data-types/string)            |
| INTEGER          | Nullable([Int32](/ko/reference/data-types/int-uint)) |
| ARRAY            | [Array](/ko/reference/data-types/array)              |

<div id="examples-of-use">
  ## 사용 예시
</div>

ClickHouse의 데이터베이스가 PostgreSQL 서버와 데이터를 교환하는 경우:

```sql theme={null}
CREATE DATABASE test_database
ENGINE = PostgreSQL('postgres1:5432', 'test_database', 'postgres', 'mysecretpassword', 'schema_name',1);
```

```sql theme={null}
SHOW DATABASES;
```

```text theme={null}
┌─name──────────┐
│ default       │
│ test_database │
│ system        │
└───────────────┘
```

```sql theme={null}
SHOW TABLES FROM test_database;
```

```text theme={null}
┌─name───────┐
│ test_table │
└────────────┘
```

PostgreSQL 테이블에서 데이터 읽기:

```sql theme={null}
SELECT * FROM test_database.test_table;
```

```text theme={null}
┌─id─┬─value─┐
│  1 │     2 │
└────┴───────┘
```

PostgreSQL 테이블에 데이터 쓰기:

```sql theme={null}
INSERT INTO test_database.test_table VALUES (3,4);
SELECT * FROM test_database.test_table;
```

```text theme={null}
┌─int_id─┬─value─┐
│      1 │     2 │
│      3 │     4 │
└────────┴───────┘
```

PostgreSQL에서 테이블 구조가 변경된 경우를 생각해 보겠습니다:

```sql theme={null}
postgre> ALTER TABLE test_table ADD COLUMN data Text
```

데이터베이스를 생성할 때 `use_table_cache` 매개변수를 `1`로 설정했으므로 ClickHouse의 테이블 구조가 캐시되어, 수정되지 않았습니다:

```sql theme={null}
DESCRIBE TABLE test_database.test_table;
```

```text theme={null}
┌─name───┬─type──────────────┐
│ id     │ Nullable(Integer) │
│ value  │ Nullable(Integer) │
└────────┴───────────────────┘
```

테이블을 DETACH한 후 다시 ATTACH하니 구조가 업데이트되었습니다:

```sql theme={null}
DETACH TABLE test_database.test_table;
ATTACH TABLE test_database.test_table;
DESCRIBE TABLE test_database.test_table;
```

```text theme={null}
┌─name───┬─type──────────────┐
│ id     │ Nullable(Integer) │
│ value  │ Nullable(Integer) │
│ data   │ Nullable(String)  │
└────────┴───────────────────┘
```

<div id="related-content">
  ## 관련 콘텐츠
</div>

* 블로그: [ClickHouse와 PostgreSQL - 데이터 세계의 찰떡궁합 - 1부](https://clickhouse.com/blog/migrating-data-between-clickhouse-postgres)
* 블로그: [ClickHouse와 PostgreSQL - 데이터 세계의 찰떡궁합 - 2부](https://clickhouse.com/blog/migrating-data-between-clickhouse-postgres-part-2)
