1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>8.9. Network Address Types</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="datatype-geometric.html" title="8.8. Geometric Types" /><link rel="next" href="datatype-bit.html" title="8.10. Bit String Types" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">8.9. Network Address Types</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="datatype-geometric.html" title="8.8. Geometric Types">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><th width="60%" align="center">Chapter 8. Data Types</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="datatype-bit.html" title="8.10. Bit String Types">Next</a></td></tr></table><hr /></div><div class="sect1" id="DATATYPE-NET-TYPES"><div class="titlepage"><div><div><h2 class="title" style="clear: both">8.9. Network Address Types <a href="#DATATYPE-NET-TYPES" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="datatype-net-types.html#DATATYPE-INET">8.9.1. <code class="type">inet</code></a></span></dt><dt><span class="sect2"><a href="datatype-net-types.html#DATATYPE-CIDR">8.9.2. <code class="type">cidr</code></a></span></dt><dt><span class="sect2"><a href="datatype-net-types.html#DATATYPE-INET-VS-CIDR">8.9.3. <code class="type">inet</code> vs. <code class="type">cidr</code></a></span></dt><dt><span class="sect2"><a href="datatype-net-types.html#DATATYPE-MACADDR">8.9.4. <code class="type">macaddr</code></a></span></dt><dt><span class="sect2"><a href="datatype-net-types.html#DATATYPE-MACADDR8">8.9.5. <code class="type">macaddr8</code></a></span></dt></dl></div><a id="id-1.5.7.17.2" class="indexterm"></a><p>
3 <span class="productname">PostgreSQL</span> offers data types to store IPv4, IPv6, and MAC
4 addresses, as shown in <a class="xref" href="datatype-net-types.html#DATATYPE-NET-TYPES-TABLE" title="Table 8.21. Network Address Types">Table 8.21</a>. It
5 is better to use these types instead of plain text types to store
6 network addresses, because
7 these types offer input error checking and specialized
8 operators and functions (see <a class="xref" href="functions-net.html" title="9.12. Network Address Functions and Operators">Section 9.12</a>).
9 </p><div class="table" id="DATATYPE-NET-TYPES-TABLE"><p class="title"><strong>Table 8.21. Network Address Types</strong></p><div class="table-contents"><table class="table" summary="Network Address Types" border="1"><colgroup><col class="col1" /><col class="col2" /><col class="col3" /></colgroup><thead><tr><th>Name</th><th>Storage Size</th><th>Description</th></tr></thead><tbody><tr><td><code class="type">cidr</code></td><td>7 or 19 bytes</td><td>IPv4 and IPv6 networks</td></tr><tr><td><code class="type">inet</code></td><td>7 or 19 bytes</td><td>IPv4 and IPv6 hosts and networks</td></tr><tr><td><code class="type">macaddr</code></td><td>6 bytes</td><td>MAC addresses</td></tr><tr><td><code class="type">macaddr8</code></td><td>8 bytes</td><td>MAC addresses (EUI-64 format)</td></tr></tbody></table></div></div><br class="table-break" /><p>
10 When sorting <code class="type">inet</code> or <code class="type">cidr</code> data types,
11 IPv4 addresses will always sort before IPv6 addresses, including
12 IPv4 addresses encapsulated or mapped to IPv6 addresses, such as
13 ::10.2.3.4 or ::ffff:10.4.3.2.
14 </p><div class="sect2" id="DATATYPE-INET"><div class="titlepage"><div><div><h3 class="title">8.9.1. <code class="type">inet</code> <a href="#DATATYPE-INET" class="id_link">#</a></h3></div></div></div><a id="id-1.5.7.17.6.2" class="indexterm"></a><p>
15 The <code class="type">inet</code> type holds an IPv4 or IPv6 host address, and
16 optionally its subnet, all in one field.
17 The subnet is represented by the number of network address bits
18 present in the host address (the
19 <span class="quote">“<span class="quote">netmask</span>”</span>). If the netmask is 32 and the address is IPv4,
20 then the value does not indicate a subnet, only a single host.
21 In IPv6, the address length is 128 bits, so 128 bits specify a
22 unique host address. Note that if you
23 want to accept only networks, you should use the
24 <code class="type">cidr</code> type rather than <code class="type">inet</code>.
26 The input format for this type is
27 <em class="replaceable"><code>address/y</code></em>
29 <em class="replaceable"><code>address</code></em>
30 is an IPv4 or IPv6 address and
31 <em class="replaceable"><code>y</code></em>
32 is the number of bits in the netmask. If the
33 <em class="replaceable"><code>/y</code></em>
34 portion is omitted, the
35 netmask is taken to be 32 for IPv4 or 128 for IPv6,
36 so the value represents
37 just a single host. On display, the
38 <em class="replaceable"><code>/y</code></em>
39 portion is suppressed if the netmask specifies a single host.
40 </p></div><div class="sect2" id="DATATYPE-CIDR"><div class="titlepage"><div><div><h3 class="title">8.9.2. <code class="type">cidr</code> <a href="#DATATYPE-CIDR" class="id_link">#</a></h3></div></div></div><a id="id-1.5.7.17.7.2" class="indexterm"></a><p>
41 The <code class="type">cidr</code> type holds an IPv4 or IPv6 network specification.
42 Input and output formats follow Classless Internet Domain Routing
44 The format for specifying networks is <em class="replaceable"><code>address/y</code></em> where <em class="replaceable"><code>address</code></em> is the network's lowest
45 address represented as an
46 IPv4 or IPv6 address, and <em class="replaceable"><code>y</code></em> is the number of bits in the netmask. If
47 <em class="replaceable"><code>y</code></em> is omitted, it is calculated
48 using assumptions from the older classful network numbering system, except
49 it will be at least large enough to include all of the octets
50 written in the input. It is an error to specify a network address
51 that has bits set to the right of the specified netmask.
53 <a class="xref" href="datatype-net-types.html#DATATYPE-NET-CIDR-TABLE" title="Table 8.22. cidr Type Input Examples">Table 8.22</a> shows some examples.
54 </p><div class="table" id="DATATYPE-NET-CIDR-TABLE"><p class="title"><strong>Table 8.22. <code class="type">cidr</code> Type Input Examples</strong></p><div class="table-contents"><table class="table" summary="cidr Type Input Examples" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th><code class="type">cidr</code> Input</th><th><code class="type">cidr</code> Output</th><th><code class="literal"><code class="function">abbrev(<code class="type">cidr</code>)</code></code></th></tr></thead><tbody><tr><td>192.168.100.128/25</td><td>192.168.100.128/25</td><td>192.168.100.128/25</td></tr><tr><td>192.168/24</td><td>192.168.0.0/24</td><td>192.168.0/24</td></tr><tr><td>192.168/25</td><td>192.168.0.0/25</td><td>192.168.0.0/25</td></tr><tr><td>192.168.1</td><td>192.168.1.0/24</td><td>192.168.1/24</td></tr><tr><td>192.168</td><td>192.168.0.0/24</td><td>192.168.0/24</td></tr><tr><td>128.1</td><td>128.1.0.0/16</td><td>128.1/16</td></tr><tr><td>128</td><td>128.0.0.0/16</td><td>128.0/16</td></tr><tr><td>128.1.2</td><td>128.1.2.0/24</td><td>128.1.2/24</td></tr><tr><td>10.1.2</td><td>10.1.2.0/24</td><td>10.1.2/24</td></tr><tr><td>10.1</td><td>10.1.0.0/16</td><td>10.1/16</td></tr><tr><td>10</td><td>10.0.0.0/8</td><td>10/8</td></tr><tr><td>10.1.2.3/32</td><td>10.1.2.3/32</td><td>10.1.2.3/32</td></tr><tr><td>2001:4f8:3:ba::/64</td><td>2001:4f8:3:ba::/64</td><td>2001:4f8:3:ba/64</td></tr><tr><td>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</td><td>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</td><td>2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128</td></tr><tr><td>::ffff:1.2.3.0/120</td><td>::ffff:1.2.3.0/120</td><td>::ffff:1.2.3/120</td></tr><tr><td>::ffff:1.2.3.0/128</td><td>::ffff:1.2.3.0/128</td><td>::ffff:1.2.3.0/128</td></tr></tbody></table></div></div><br class="table-break" /></div><div class="sect2" id="DATATYPE-INET-VS-CIDR"><div class="titlepage"><div><div><h3 class="title">8.9.3. <code class="type">inet</code> vs. <code class="type">cidr</code> <a href="#DATATYPE-INET-VS-CIDR" class="id_link">#</a></h3></div></div></div><p>
55 The essential difference between <code class="type">inet</code> and <code class="type">cidr</code>
56 data types is that <code class="type">inet</code> accepts values with nonzero bits to
57 the right of the netmask, whereas <code class="type">cidr</code> does not. For
58 example, <code class="literal">192.168.0.1/24</code> is valid for <code class="type">inet</code>
59 but not for <code class="type">cidr</code>.
60 </p><div class="tip"><h3 class="title">Tip</h3><p>
61 If you do not like the output format for <code class="type">inet</code> or
62 <code class="type">cidr</code> values, try the functions <code class="function">host</code>,
63 <code class="function">text</code>, and <code class="function">abbrev</code>.
64 </p></div></div><div class="sect2" id="DATATYPE-MACADDR"><div class="titlepage"><div><div><h3 class="title">8.9.4. <code class="type">macaddr</code> <a href="#DATATYPE-MACADDR" class="id_link">#</a></h3></div></div></div><a id="id-1.5.7.17.9.2" class="indexterm"></a><a id="id-1.5.7.17.9.3" class="indexterm"></a><p>
65 The <code class="type">macaddr</code> type stores MAC addresses, known for example
66 from Ethernet card hardware addresses (although MAC addresses are
67 used for other purposes as well). Input is accepted in the
70 </p><table border="0" summary="Simple list" class="simplelist"><tr><td><code class="literal">'08:00:2b:01:02:03'</code></td></tr><tr><td><code class="literal">'08-00-2b-01-02-03'</code></td></tr><tr><td><code class="literal">'08002b:010203'</code></td></tr><tr><td><code class="literal">'08002b-010203'</code></td></tr><tr><td><code class="literal">'0800.2b01.0203'</code></td></tr><tr><td><code class="literal">'0800-2b01-0203'</code></td></tr><tr><td><code class="literal">'08002b010203'</code></td></tr></table><p>
72 These examples all specify the same address. Upper and
73 lower case is accepted for the digits
74 <code class="literal">a</code> through <code class="literal">f</code>. Output is always in the
75 first of the forms shown.
77 IEEE Standard 802-2001 specifies the second form shown (with hyphens)
78 as the canonical form for MAC addresses, and specifies the first
79 form (with colons) as used with bit-reversed, MSB-first notation, so that
80 08-00-2b-01-02-03 = 10:00:D4:80:40:C0. This convention is widely
81 ignored nowadays, and it is relevant only for obsolete network
82 protocols (such as Token Ring). PostgreSQL makes no provisions
83 for bit reversal; all accepted formats use the canonical LSB
86 The remaining five input formats are not part of any standard.
87 </p></div><div class="sect2" id="DATATYPE-MACADDR8"><div class="titlepage"><div><div><h3 class="title">8.9.5. <code class="type">macaddr8</code> <a href="#DATATYPE-MACADDR8" class="id_link">#</a></h3></div></div></div><a id="id-1.5.7.17.10.2" class="indexterm"></a><a id="id-1.5.7.17.10.3" class="indexterm"></a><p>
88 The <code class="type">macaddr8</code> type stores MAC addresses in EUI-64
89 format, known for example from Ethernet card hardware addresses
90 (although MAC addresses are used for other purposes as well).
91 This type can accept both 6 and 8 byte length MAC addresses
92 and stores them in 8 byte length format. MAC addresses given
93 in 6 byte format will be stored in 8 byte length format with the
94 4th and 5th bytes set to FF and FE, respectively.
96 Note that IPv6 uses a modified EUI-64 format where the 7th bit
97 should be set to one after the conversion from EUI-48. The
98 function <code class="function">macaddr8_set7bit</code> is provided to make this
101 Generally speaking, any input which is comprised of pairs of hex
102 digits (on byte boundaries), optionally separated consistently by
103 one of <code class="literal">':'</code>, <code class="literal">'-'</code> or <code class="literal">'.'</code>, is
104 accepted. The number of hex digits must be either 16 (8 bytes) or
105 12 (6 bytes). Leading and trailing whitespace is ignored.
107 The following are examples of input formats that are accepted:
109 </p><table border="0" summary="Simple list" class="simplelist"><tr><td><code class="literal">'08:00:2b:01:02:03:04:05'</code></td></tr><tr><td><code class="literal">'08-00-2b-01-02-03-04-05'</code></td></tr><tr><td><code class="literal">'08002b:0102030405'</code></td></tr><tr><td><code class="literal">'08002b-0102030405'</code></td></tr><tr><td><code class="literal">'0800.2b01.0203.0405'</code></td></tr><tr><td><code class="literal">'0800-2b01-0203-0405'</code></td></tr><tr><td><code class="literal">'08002b01:02030405'</code></td></tr><tr><td><code class="literal">'08002b0102030405'</code></td></tr></table><p>
111 These examples all specify the same address. Upper and
112 lower case is accepted for the digits
113 <code class="literal">a</code> through <code class="literal">f</code>. Output is always in the
114 first of the forms shown.
116 The last six input formats shown above are not part of any standard.
118 To convert a traditional 48 bit MAC address in EUI-48 format to
119 modified EUI-64 format to be included as the host portion of an
120 IPv6 address, use <code class="function">macaddr8_set7bit</code> as shown:
122 </p><pre class="programlisting">
123 SELECT macaddr8_set7bit('08:00:2b:01:02:03');
124 <code class="computeroutput">
126 -------------------------
127 0a:00:2b:ff:fe:01:02:03
132 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="datatype-geometric.html" title="8.8. Geometric Types">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="datatype-bit.html" title="8.10. Bit String Types">Next</a></td></tr><tr><td width="40%" align="left" valign="top">8.8. Geometric Types </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 8.10. Bit String Types</td></tr></table></div></body></html>