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

# 8.9. 網路位址型別 \#

### 8.9. 網路位址型別 [#](#datatype-net-types) <a href="#datatype-net-types" id="datatype-net-types"></a>

[8.9.1. `inet`](#datatype-inet)

[8.9.2. `cidr`](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-net-types/datatype-cidr.md#datatype-cidr)

[8.9.3. `inet` 與 `cidr` 的比較](#datatype-inet-vs-cidr)

[8.9.4. `macaddr`](#datatype-macaddr)

[8.9.5. `macaddr8`](#datatype-macaddr8)

PostgreSQL 提供了用來儲存 IPv4、IPv6 與 MAC 位址的資料型別，如[表 8.21](#datatype-net-types-table) 所示。儲存網路位址時，使用這些型別會比使用單純的文字型別更好，因為這些型別提供了輸入的錯誤檢查，以及專門的運算子與函式（請參閱[第 9.12 節](https://docs.postgresql.tw/19/the-sql-language/functions/functions-net.md)）。

#### 表 8.21. 網路位址型別 [#](#datatype-net-types-table) <a href="#datatype-net-types-table" id="datatype-net-types-table"></a>

| 名稱         | 儲存空間大小      | 說明                |
| ---------- | ----------- | ----------------- |
| `cidr`     | 7 或 19 個位元組 | IPv4 與 IPv6 網路    |
| `inet`     | 7 或 19 個位元組 | IPv4 與 IPv6 主機與網路 |
| `macaddr`  | 6 個位元組      | MAC 位址            |
| `macaddr8` | 8 個位元組      | MAC 位址（EUI-64 格式） |

<br>

在對 `inet` 或 `cidr` 資料型別排序時，IPv4 位址一律會排在 IPv6 位址之前，包括那些被封裝或對映到 IPv6 位址的 IPv4 位址，例如 ::10.2.3.4 或 ::ffff:10.4.3.2。

#### 8.9.1. `inet` [#](#datatype-inet) <a href="#datatype-inet" id="datatype-inet"></a>

`inet` 型別在同一個欄位中存放一個 IPv4 或 IPv6 主機位址，以及選擇性的子網路資訊。子網路是以該主機位址中屬於網路位址的位元數（即「網路遮罩」）來表示。如果網路遮罩是 32 而位址是 IPv4，那麼這個值並不表示一個子網路，而只表示單一主機。在 IPv6 中，位址長度是 128 位元，所以 128 位元就指定了一個唯一的主機位址。請注意，如果你只想接受網路，應該使用 `cidr` 型別而不是 `inet`。

這個型別的輸入格式是 *`address/y`*，其中 *`address`* 是 IPv4 或 IPv6 位址，而 *`y`* 則是網路遮罩的位元數。如果省略 *`/y`* 的部分，網路遮罩會被視為 IPv4 的 32 或 IPv6 的 128，因此該值就只代表單一主機。顯示時，如果網路遮罩所指定的是單一主機，*`/y`* 的部分會被省略。

#### 8.9.2. `cidr` [#](#datatype-cidr) <a href="#datatype-cidr" id="datatype-cidr"></a>

完整內容請見 [8.9.2. cidr](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-net-types/datatype-cidr.md#datatype-cidr)。

#### 8.9.3. `inet` 與 `cidr` 的比較 [#](#datatype-inet-vs-cidr) <a href="#datatype-inet-vs-cidr" id="datatype-inet-vs-cidr"></a>

`inet` 與 `cidr` 這兩種資料型別最主要的差別在於：`inet` 接受在網路遮罩右側含有非零位元的值，而 `cidr` 則不接受。例如 `192.168.0.1/24` 對 `inet` 而言是有效的，但對 `cidr` 而言則否。

#### 提示

如果你不喜歡 `inet` 或 `cidr` 值的輸出格式，可以試試 `host`、`text` 與 `abbrev` 這幾個函式。

#### 8.9.4. `macaddr` [#](#datatype-macaddr) <a href="#datatype-macaddr" id="datatype-macaddr"></a>

`macaddr` 型別儲存 MAC 位址，這種位址最為人所知的例子就是乙太網路卡的硬體位址（不過 MAC 位址也用於其他用途）。輸入時可接受以下格式：

| `'08:00:2b:01:02:03'` |
| --------------------- |
| `'08-00-2b-01-02-03'` |
| `'08002b:010203'`     |
| `'08002b-010203'`     |
| `'0800.2b01.0203'`    |
| `'0800-2b01-0203'`    |
| `'08002b010203'`      |

這些範例指定的都是同一個位址。數字 `a` 到 `f` 大小寫皆可接受。輸出一律採用所示格式中的第一種。

IEEE Standard 802-2001 把所示的第二種格式（使用連字號）規定為 MAC 位址的標準格式，並規定第一種格式（使用冒號）用於位元反轉、MSB 在前的表示法，因此 08-00-2b-01-02-03 = 10:00:D4:80:40:C0。這個慣例如今普遍不被採用，而且只與過時的網路通訊協定（例如 Token Ring）有關。PostgreSQL 並未提供任何位元反轉的機制；所有可接受的格式都採用標準的 LSB 順序。

其餘五種輸入格式並不屬於任何標準。

#### 8.9.5. `macaddr8` [#](#datatype-macaddr8) <a href="#datatype-macaddr8" id="datatype-macaddr8"></a>

`macaddr8` 型別以 EUI-64 格式儲存 MAC 位址，這種位址最為人所知的例子就是乙太網路卡的硬體位址（不過 MAC 位址也用於其他用途）。這個型別可以接受 6 個位元組與 8 個位元組長度的 MAC 位址，並以 8 個位元組的長度格式儲存它們。以 6 個位元組格式給定的 MAC 位址會以 8 個位元組的長度格式儲存，其中第 4 與第 5 個位元組分別設為 FF 與 FE。請注意，IPv6 使用的是經過修改的 EUI-64 格式，在由 EUI-48 轉換之後第 7 個位元應該設為 1。系統提供了 `macaddr8_set7bit` 函式來進行這項變更。一般而言，只要輸入是由成對的十六進位數字（以位元組為界）所組成，並可選擇性地一致以 `':'`、`'-'` 或 `'.'` 其中之一分隔，都可以被接受。十六進位數字的個數必須是 16（8 個位元組）或 12（6 個位元組）。開頭與結尾的空白字元會被忽略。以下是可接受的輸入格式範例：

| `'08:00:2b:01:02:03:04:05'` |
| --------------------------- |
| `'08-00-2b-01-02-03-04-05'` |
| `'08002b:0102030405'`       |
| `'08002b-0102030405'`       |
| `'0800.2b01.0203.0405'`     |
| `'0800-2b01-0203-0405'`     |
| `'08002b01:02030405'`       |
| `'08002b0102030405'`        |

這些範例指定的都是同一個位址。數字 `a` 到 `f` 大小寫皆可接受。輸出一律採用所示格式中的第一種。

上面所示的最後六種輸入格式並不屬於任何標準。

若要把傳統的 48 位元 EUI-48 格式 MAC 位址轉換成經過修改的 EUI-64 格式，以便作為 IPv6 位址的主機部分，請依下列方式使用 `macaddr8_set7bit`：

```

SELECT macaddr8_set7bit('08:00:2b:01:02:03');

    macaddr8_set7bit
-------------------------
 0a:00:2b:ff:fe:01:02:03
(1 row)
```

***

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