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

# 8.4. 二進位資料型別 \#

### 8.4. 二進位資料型別 [#](#datatype-binary) <a href="#datatype-binary" id="datatype-binary"></a>

[8.4.1. `bytea` 十六進位格式](#datatype-binary-bytea-hex-format)

[8.4.2. `bytea` 跳脫格式](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-binary/datatype-binary-bytea-escape-format.md#datatype-binary-bytea-escape-format)

`bytea` 資料型別可以用來儲存二進位字串；請參閱[表 8.6](#datatype-binary-table)。

#### 表 8.6. 二進位資料型別 [#](#datatype-binary-table) <a href="#datatype-binary-table" id="datatype-binary-table"></a>

| 名稱      | 儲存空間大小               | 說明         |
| ------- | -------------------- | ---------- |
| `bytea` | 1 或 4 個位元組加上實際的二進位字串 | 可變長度的二進位字串 |

<br>

二進位字串是由一連串的位元組（octet，或稱 byte）所組成。二進位字串與字元字串有兩點不同。第一，二進位字串明確允許儲存值為零的位元組，以及其他「不可列印」的位元組（通常是十進位 32 到 126 範圍之外的位元組）。字元字串不允許零位元組，也不允許任何其他依資料庫所選用的字元集編碼而言屬於無效的位元組值與位元組值序列。第二，對二進位字串的操作處理的是實際的位元組，而字元字串的處理則取決於語系設定。簡而言之，二進位字串適合用來儲存程式設計者視為「原始位元組」的資料，而字元字串則適合用來儲存文字。

`bytea` 型別的輸入與輸出支援兩種格式：「十六進位」格式，以及 PostgreSQL 沿用已久的「跳脫」格式。這兩種格式在輸入時一律都會被接受。輸出格式則取決於組態參數 [bytea\_output](https://docs.postgresql.tw/19/server-administration/runtime-config/runtime-config-client.md#GUC-BYTEA-OUTPUT)，預設值是十六進位。（請注意，十六進位格式是在 PostgreSQL 9.0 引入的；較早的版本與某些工具並不認得它。）

SQL 標準定義了另一種不同的二進位字串型別，稱為 `BLOB` 或 `BINARY LARGE OBJECT`。其輸入格式與 `bytea` 不同，但所提供的函式與運算子則大致相同。

#### 8.4.1. `bytea` 十六進位格式 [#](#datatype-binary-bytea-hex-format) <a href="#datatype-binary-bytea-hex-format" id="datatype-binary-bytea-hex-format"></a>

「十六進位」格式會把二進位資料編碼成每個位元組 2 個十六進位數字，高位的半位元組（nibble）在前。整個字串前面會加上 `\x` 序列（用來與跳脫格式區分）。在某些情況下，開頭的反斜線可能需要以重複兩次的方式來跳脫（請參閱[第 4.1.2.1 節](https://docs.postgresql.tw/19/the-sql-language/sql-syntax/sql-syntax-lexical/sql-syntax-constants.md#sql-syntax-strings)）。在輸入時，十六進位數字可以是大寫或小寫，而且數字對之間允許空白字元（但數字對內部以及開頭的 `\x` 序列中不允許）。十六進位格式與各式各樣的外部應用程式與通訊協定相容，而且轉換速度通常比跳脫格式快，因此建議優先使用。

範例：

```

SET bytea_output = 'hex';

SELECT '\xDEADBEEF'::bytea;
   bytea
------------
 \xdeadbeef
```

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

完整內容請見 [8.4.2. bytea 跳脫格式](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-binary/datatype-binary-bytea-escape-format.md#datatype-binary-bytea-escape-format)。

***

原文：[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.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.
