> 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/encryption-options.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 18.8. 加密選項 \#

### 18.8. 加密選項 [#](#encryption-options) <a href="#encryption-options" id="encryption-options"></a>

PostgreSQL 在多個層級提供加密功能， 讓您能夠靈活地保護資料，避免因資料庫伺服器遭竊、 管理者行為不當，或網路不安全，而導致資料外洩。 要保護諸如病歷或財務交易等敏感資料， 也可能需要用到加密功能。

密碼加密

資料庫使用者的密碼，會以雜湊值的形式儲存 （由 [password\_encryption](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-connection.md#GUC-PASSWORD-ENCRYPTION) 設定決定），因此管理者無法得知指派給使用者的實際密碼。 若用戶端驗證使用 SCRAM 或 MD5 加密， 則未加密的密碼，甚至不會暫時出現在伺服器上， 因為用戶端會在透過網路傳送之前，先對密碼進行加密。 建議優先使用 SCRAM，因為它是一項網際網路標準， 比 PostgreSQL 專屬的 MD5 驗證協定更安全。

#### 警告 [#](#encryption-options-restored-note-1) <a href="#encryption-options-restored-note-1" id="encryption-options-restored-note-1"></a>

對 MD5 加密密碼的支援，已被棄用， 未來的 PostgreSQL 版本將會將其移除。 關於遷移至其他密碼型別的詳情， 請參閱[20.5 節](https://docs.postgresql.tw/19/server-administration/client-authentication/auth-password.md)。

針對特定欄位加密

[pgcrypto](https://docs.postgresql.tw/19/appendixes/contrib/pgcrypto.md) 模組， 可讓特定欄位以加密的形式儲存。 這在只有部分資料屬於敏感資料時相當實用。 用戶端會提供解密金鑰，資料會在伺服器上解密， 然後再傳送給用戶端。

在資料被解密、並於用戶端與伺服器之間傳輸的短暫期間， 解密後的資料與解密金鑰，會存在於伺服器上。 這會產生一段短暫的時機，讓具有資料庫伺服器完整存取權限的人 （例如系統管理者），有機會攔截這些資料與金鑰。

資料分割區加密

儲存體加密，可以在檔案系統層級或區塊層級進行。 Linux 的檔案系統加密選項， 包括 eCryptfs 與 EncFS，而 FreeBSD 則使用 PEFS。 區塊層級或全磁碟加密選項， 則包括 Linux 上的 dm-crypt + LUKS， 以及 FreeBSD 上的 GEOM 模組 geli 與 gbde。 許多其他作業系統，包括 Windows， 也都支援這項功能。

這種機制，可以防止在磁碟機或整部電腦遭竊時， 讀取到未加密的資料。但它無法防範檔案系統 掛載期間所發生的攻擊，因為在掛載狀態下， 作業系統會提供未加密的資料檢視畫面。不過， 要掛載檔案系統，您需要某種方式， 將加密金鑰傳遞給作業系統， 而有時該金鑰，會儲存在掛載該磁碟之主機上的某處。

透過網路加密資料

SSL 連線，會將透過網路傳送的所有資料加密： 包括密碼、查詢內容，以及傳回的資料。 `pg_hba.conf` 檔案，讓管理者能夠指定 哪些主機可以使用未加密的連線（`host`）， 哪些主機則要求使用 SSL 加密連線 （`hostssl`）。此外，用戶端也可以指定， 只透過 SSL 連線至伺服器。

GSSAPI 加密連線，會將透過網路傳送的所有資料加密， 包括查詢內容與傳回的資料。（網路上不會傳送密碼。） `pg_hba.conf` 檔案，讓管理者能夠指定 哪些主機可以使用未加密的連線（`host`）， 哪些主機則要求使用 GSSAPI 加密連線 （`hostgssenc`）。此外，用戶端也可以指定， 只透過 GSSAPI 加密連線至伺服器 （`gssencmode=require`）。

也可以使用 Stunnel 或 SSH， 來加密傳輸內容。

SSL 主機驗證 : 用戶端與伺服器，可以互相向對方提供 SSL 憑證。 這需要在雙方各自進行一些額外的設定， 但相較於僅使用密碼，這能提供更強的身分驗證。 它能防止某部電腦，假冒成伺服器， 只為了讀取用戶端傳送的密碼。這也有助於防範 「中間人」（man in the middle）攻擊， 也就是位於用戶端與伺服器之間的某部電腦， 假冒成伺服器，讀取並轉發用戶端與伺服器之間 的所有資料。

用戶端加密 : 若無法信任伺服器所在機器的系統管理者， 就必須由用戶端對資料進行加密；如此一來， 未加密的資料，就永遠不會出現在資料庫伺服器上。 資料會在用戶端上加密之後，才傳送至伺服器， 而資料庫傳回的結果，也必須在用戶端上解密之後， 才能使用。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/encryption-options.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/encryption-options.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.
