> 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/the-sql-language/datatype/datatype-binary/datatype-binary-bytea-escape-format.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 8.4.2. bytea 跳脫格式

## 8.4.2. `bytea` 跳脫格式 [#](#datatype-binary-bytea-escape-format) <a href="#datatype-binary-bytea-escape-format" id="datatype-binary-bytea-escape-format"></a>

「跳脫」格式是 PostgreSQL 對 `bytea` 型別的傳統格式。它採取的做法是把二進位字串表示成一連串的 ASCII 字元，同時把那些無法以 ASCII 字元表示的位元組轉換成特殊的跳脫序列。如果從應用程式的角度來看，把位元組表示成字元是合理的，那麼這種表示法可能會很方便。但實務上它通常令人困惑，因為它模糊了二進位字串與字元字串之間的區別，而且所選用的那套跳脫機制也有些笨拙。因此，大多數新的應用程式大概都應該避免使用這種格式。

以跳脫格式輸入 `bytea` 值時，某些值的位元組*必須*跳脫，而所有位元組值都*可以*跳脫。一般而言，要跳脫一個位元組，就把它轉換成三位數的八進位值，並在前面加上一個反斜線。反斜線本身（十進位位元組值 92）也可以改用兩個反斜線來表示。[表 8.7](#datatype-binary-sqlesc) 列出必須跳脫的字元，並在適用時給出可替代的跳脫序列。

### 表 8.7. `bytea` 常數中的跳脫位元組 [#](#datatype-binary-sqlesc) <a href="#datatype-binary-sqlesc" id="datatype-binary-sqlesc"></a>

| 十進位位元組值             | 說明         | 跳脫後的輸入表示法          | 範例              | 十六進位表示法 |
| ------------------- | ---------- | ------------------ | --------------- | ------- |
| 0                   | 零位元組       | `'\000'`           | `'\000'::bytea` | `\x00`  |
| 39                  | 單引號        | `''''` 或 `'\047'`  | `''''::bytea`   | `\x27`  |
| 92                  | 反斜線        | `'\\'` 或 `'\134'`  | `'\\'::bytea`   | `\x5c`  |
| 0 到 31 以及 127 到 255 | 「不可列印」的位元組 | `'\`*`xxx'`*（八進位值） | `'\001'::bytea` | `\x01`  |

<br>

*不可列印*位元組是否必須跳脫，會因語系設定而異。在某些情況下，你可以不跳脫它們也沒問題。

如同[表 8.7](#datatype-binary-sqlesc) 所示，單引號必須重複兩次的原因在於：這對 SQL 指令中的任何字串常數都成立。一般性的字串常數剖析器會消耗掉最外層的單引號，並把每一對單引號縮減成一個資料字元。`bytea` 輸入函式所看到的就只是一個單引號，而它會把那個單引號當成一般的資料字元。不過，`bytea` 輸入函式會把反斜線視為特殊字元，而[表 8.7](#datatype-binary-sqlesc) 中所示的其他行為都是由該函式所實作的。

在某些情況下，反斜線必須比上面所示的再多重複一次，因為一般性的字串常數剖析器也會把每一對反斜線縮減成一個資料字元；請參閱[第 4.1.2.1 節](https://docs.postgresql.tw/19/the-sql-language/sql-syntax/sql-syntax-lexical/sql-syntax-constants.md#sql-syntax-strings)。

`Bytea` 的位元組預設會以 `hex` 格式輸出。如果你把 [bytea\_output](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-client.md#GUC-BYTEA-OUTPUT) 改成 `escape`，「不可列印」的位元組會被轉換成等價的三位數八進位值，並在前面加上一個反斜線。大多數「可列印」的位元組則會以它們在用戶端字元集中的標準表示法輸出，例如：

```

SET bytea_output = 'escape';

SELECT 'abc \153\154\155 \052\251\124'::bytea;
     bytea
----------------
 abc klm *\251T
```

十進位值為 92 的位元組（反斜線）在輸出時會重複兩次。細節請參閱[表 8.8](#datatype-binary-resesc)。

### 表 8.8. `bytea` 輸出的跳脫位元組 [#](#datatype-binary-resesc) <a href="#datatype-binary-resesc" id="datatype-binary-resesc"></a>

| 十進位位元組值             | 說明         | 跳脫後的輸出表示法        | 範例              | 輸出結果   |
| ------------------- | ---------- | ---------------- | --------------- | ------ |
| 92                  | 反斜線        | `\\`             | `'\134'::bytea` | `\\`   |
| 0 到 31 以及 127 到 255 | 「不可列印」的位元組 | `\`*`xxx`*（八進位值） | `'\001'::bytea` | `\001` |
| 32 至 126            | 「可列印」的位元組  | 用戶端字元集的表示法       | `'\176'::bytea` | `~`    |

<br>

視你所使用的 PostgreSQL 前端而定，在跳脫與還原 `bytea` 字串方面你可能還得多做一些工作。例如，如果你的介面會自動轉換換行字元與歸位字元，你可能也必須把它們跳脫。

[返回 8.4. 二進位資料型別](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-binary.md#datatype-binary)

***

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


---

# 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/the-sql-language/datatype/datatype-binary/datatype-binary-bytea-escape-format.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.
