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

# SET SESSION AUTHORIZATION

### SET SESSION AUTHORIZATION

SET SESSION AUTHORIZATION — 設定目前工作階段的工作階段使用者識別碼與目前使用者識別碼

### 語法

```

SET [ SESSION | LOCAL ] SESSION AUTHORIZATION user_name
SET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT
RESET SESSION AUTHORIZATION
```

### 說明

此命令會將目前 SQL 工作階段的工作階段使用者識別碼與目前使用者識別碼設為 *`user_name`*。使用者名稱可以寫成識別字，也可以寫成字串常值。 使用此命令，舉例來說，可以暫時變成一個沒有權限的使用者，之後再切換回超級使用者。

工作階段使用者識別碼一開始會設為用戶端所提供的（可能經過驗證的）使用者名稱。 目前使用者識別碼通常等於工作階段使用者識別碼，但在 `SECURITY DEFINER` 函式及類似機制的情境下可能會暫時改變； 它也可以透過 [`SET ROLE`](https://docs.postgresql.tw/19/reference/sql-commands/sql-set-role.md) 來變更。 目前使用者識別碼與權限檢查有關。

只有在最初的工作階段使用者（*已驗證使用者*）具有超級使用者權限時， 才能變更工作階段使用者識別碼。否則，只有在此命令指定的正是已驗證使用者名稱時， 才會被接受。

`SESSION` 與 `LOCAL` 修飾詞的作用方式， 與一般 [`SET`](https://docs.postgresql.tw/19/reference/sql-commands/sql-set.md) 命令相同。

`DEFAULT` 與 `RESET` 形式會將工作階段使用者識別碼與目前使用者識別碼， 重設為最初通過驗證的使用者名稱。任何使用者都可以執行這些形式。

### 注意事項

`SET SESSION AUTHORIZATION` 無法在 `SECURITY DEFINER` 函式內使用。

### 範例

```

SELECT SESSION_USER, CURRENT_USER;

 session_user | current_user
--------------+--------------
 peter        | peter

SET SESSION AUTHORIZATION 'paul';

SELECT SESSION_USER, CURRENT_USER;

 session_user | current_user
--------------+--------------
 paul         | paul
```

### 相容性

SQL 標準允許在字面值 *`user_name`* 的位置使用其他一些運算式， 但這些選項在實務上並不重要。PostgreSQL 允許使用識別字語法（`"username"`），而 SQL 標準並不允許。 SQL 標準不允許在交易中執行此命令；PostgreSQL 並無此限制，因為沒有理由要這樣限制。 `SESSION` 與 `LOCAL` 修飾詞是 PostgreSQL 的擴充功能，`RESET` 語法亦同。

執行此命令所需的權限，標準將其留給實作自行定義。

### 另請參閱

[SET ROLE](https://docs.postgresql.tw/19/reference/sql-commands/sql-set-role.md)

***

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


---

# 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/reference/sql-commands/sql-set-session-authorization.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.
