> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/server-administration/runtime/ssh-tunnels.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 18.11. 使用 SSH 通道的安全 TCP/IP 連線 \#

### 18.11. 使用 SSH 通道的安全 TCP/IP 連線 [#](#ssh-tunnels) <a href="#ssh-tunnels" id="ssh-tunnels"></a>

您可以使用 SSH，為用戶端與 PostgreSQL 伺服器之間的 網路連線加密。只要處理得當，即使是不支援 SSL 的用戶端， 也能因此獲得足夠安全的網路連線。

首先，請確認 SSH 伺服器，在與 PostgreSQL 伺服器 相同的機器上正常執行，並且您能以某個使用者身分， 使用 `ssh` 登入；接著，您就能建立一條 通往遠端伺服器的安全通道。安全通道，會在本機的某個 連接埠上聆聽，並將所有流量，轉發至遠端機器上的某個連接埠。 傳送至遠端連接埠的流量，可以抵達其 `localhost` 位址，或（若您希望的話）其他繫結位址；它不會顯示為 來自您本機的流量。以下指令，會建立一條從用戶端機器， 通往遠端機器 `foo.com` 的安全通道：

```

ssh -L 63333:localhost:5432 joe@foo.com
```

`-L` 引數中的第一個數字 63333， 是該通道的本機連接埠編號；可以是任何未使用的連接埠。 （IANA 將連接埠 49152 至 65535 保留供私人使用。） 其後的名稱或 IP 位址，是您所要連線的遠端繫結位址， 也就是 `localhost`，這是預設值。第二個數字 5432，則是該通道的遠端端點，例如您資料庫伺服器 所使用的連接埠編號。若要透過此通道連線至資料庫伺服器， 您需要連線至本機的 63333 連接埠：

```

psql -h localhost -p 63333 postgres
```

如此一來，對資料庫伺服器而言，看起來就會像是使用者 `joe` 在主機 `foo.com` 上， 連線至 `localhost` 繫結位址一般， 並且會採用針對該使用者連線至該繫結位址所設定的 驗證程序。請注意，伺服器並不會認為此連線是以 SSL 加密的， 因為在 SSH 伺服器與 PostgreSQL 伺服器之間， 實際上並未加密。這應該不會帶來額外的安全風險， 因為它們位於同一部機器上。

為了讓通道設定能夠成功建立，您必須被允許 以 `joe@foo.com` 的身分，透過 `ssh` 連線，就如同您嘗試使用 `ssh` 建立終端機工作階段時一樣。

您也可以像這樣設定連接埠轉發：

```

ssh -L 63333:foo.com:5432 joe@foo.com
```

但如此一來，資料庫伺服器就會將此連線， 視為來自其 `foo.com` 繫結位址， 而該位址在預設設定 `listen_addresses = 'localhost'` 下，並未開啟。這通常並非您想要的結果。

若您必須透過某台登入主機，「跳轉」至資料庫伺服器， 其中一種可能的設定方式如下：

```

ssh -L 63333:db.foo.com:5432 joe@shell.foo.com
```

請注意，以這種方式，從 `shell.foo.com` 到 `db.foo.com` 之間的連線， 並不會被 SSH 通道加密。 當網路受到各種方式限制時，SSH 提供了相當多的組態設定可能性。 詳情請參閱 SSH 的相關文件。

#### 提示

還有其他幾個應用程式，能夠提供在概念上， 與上述做法類似的安全通道。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/ssh-tunnels.html)（原文版本：18.6；核對日期：2026-09-22）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/server-administration/runtime/ssh-tunnels.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
