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

# 29.11. 安全性 \#

### 29.11. 安全性 [#](#logical-replication-security) <a href="#logical-replication-security" id="logical-replication-security"></a>

用於複寫連線的角色，必須具有 `REPLICATION` 屬性（或為超級使用者）。 若該角色缺少 `SUPERUSER` 與 `BYPASSRLS`， 發布端的資料列安全性原則便可能執行。若該角色不信任所有資料表擁有者， 請在連線字串中加入 `options=-crow_security=off`； 如此一來，若日後有資料表擁有者新增資料列安全性原則， 該設定會導致複寫停止，而不會執行該原則。 此角色的存取權限必須在 `pg_hba.conf` 中設定， 且必須具有 `LOGIN` 屬性。

複寫連線所使用的輸出外掛名稱，必須包含在伺服器的 [output\_plugin\_libraries](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-replication.md#GUC-OUTPUT-PLUGIN-LIBRARIES) 中。（就訂閱而言，所使用的外掛名稱是 `pgoutput`。） 超級使用者可以在連線字串中加入 `options=-coutput_plugin_libraries=...`， 以逐連線方式修改受信任清單。

為了能夠複製初始資料表資料，用於複寫連線的角色， 必須對已發布的資料表具有 `SELECT` 權限 （或為超級使用者）。

若要建立發布，使用者必須在該資料庫中具有 `CREATE` 權限。

若要將資料表加入發布，使用者必須擁有該資料表的擁有權。 若要將某綱要（schema）中的所有資料表加入發布，使用者必須是超級使用者。 若要建立一個自動發布所有資料表、或某綱要中所有資料表的發布， 使用者也必須是超級使用者。

目前發布並沒有任何權限機制。任何（能夠連線的）訂閱都可以存取任何發布。 因此，如果你打算對特定訂閱端隱藏某些資訊——例如透過資料列篩選（row filter） 或欄位清單（column list），或是不將整個資料表加入發布—— 請務必留意，同一個資料庫中的其他發布，仍可能揭露相同的資訊。 未來 PostgreSQL 可能會加入發布權限機制， 以支援更細緻的存取控制。

若要建立訂閱，使用者必須具有 `pg_create_subscription` 角色的權限， 以及該資料庫的 `CREATE` 權限。

訂閱的套用程序（apply process），在工作階段層級上， 會以訂閱擁有者的權限執行。不過，當它對特定資料表執行插入、更新、 刪除或截斷（truncate）操作時，會切換角色為該資料表的擁有者， 並以資料表擁有者的權限執行該操作。這表示訂閱擁有者必須能夠 `SET ROLE` 為每一個擁有複寫資料表的角色。

若訂閱已設定 `run_as_owner = true`， 就不會發生使用者切換；所有操作都會以訂閱擁有者的權限執行。 在這種情況下，訂閱擁有者只需要對目標資料表具有 `SELECT`、`INSERT`、`UPDATE` 及 `DELETE` 權限，而不需要具有切換為資料表擁有者的 `SET ROLE` 權限。然而，這也表示任何擁有正在被複寫寫入資料之 資料表的使用者，都能夠以訂閱擁有者的權限執行任意程式碼—— 例如，只要在自己擁有的其中一個資料表上附加一個觸發程序即可做到。 由於通常不希望某個角色能夠自由取得另一個角色的權限， 除非資料庫內的使用者安全性並非考量重點，否則應避免使用此選項。

在發布端，權限只會在複寫連線開始時檢查一次， 之後在讀取每一筆變更記錄時並不會重新檢查。

在訂閱端，訂閱擁有者的權限會在每次套用交易時重新檢查。 如果某個工作程序正在套用交易的過程中，訂閱的擁有權被另一個並行交易變更， 則目前這筆交易的套用作業，仍會在原擁有者的權限下繼續進行。

***

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


---

# 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/logical-replication/logical-replication-security.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.
