2 8.9. Network Address Types #
10 PostgreSQL offers data types to store IPv4, IPv6, and MAC addresses, as
11 shown in Table 8.21. It is better to use these types instead of plain
12 text types to store network addresses, because these types offer input
13 error checking and specialized operators and functions (see
16 Table 8.21. Network Address Types
17 Name Storage Size Description
18 cidr 7 or 19 bytes IPv4 and IPv6 networks
19 inet 7 or 19 bytes IPv4 and IPv6 hosts and networks
20 macaddr 6 bytes MAC addresses
21 macaddr8 8 bytes MAC addresses (EUI-64 format)
23 When sorting inet or cidr data types, IPv4 addresses will always sort
24 before IPv6 addresses, including IPv4 addresses encapsulated or mapped
25 to IPv6 addresses, such as ::10.2.3.4 or ::ffff:10.4.3.2.
29 The inet type holds an IPv4 or IPv6 host address, and optionally its
30 subnet, all in one field. The subnet is represented by the number of
31 network address bits present in the host address (the “netmask”). If
32 the netmask is 32 and the address is IPv4, then the value does not
33 indicate a subnet, only a single host. In IPv6, the address length is
34 128 bits, so 128 bits specify a unique host address. Note that if you
35 want to accept only networks, you should use the cidr type rather than
38 The input format for this type is address/y where address is an IPv4 or
39 IPv6 address and y is the number of bits in the netmask. If the /y
40 portion is omitted, the netmask is taken to be 32 for IPv4 or 128 for
41 IPv6, so the value represents just a single host. On display, the /y
42 portion is suppressed if the netmask specifies a single host.
46 The cidr type holds an IPv4 or IPv6 network specification. Input and
47 output formats follow Classless Internet Domain Routing conventions.
48 The format for specifying networks is address/y where address is the
49 network's lowest address represented as an IPv4 or IPv6 address, and y
50 is the number of bits in the netmask. If y is omitted, it is calculated
51 using assumptions from the older classful network numbering system,
52 except it will be at least large enough to include all of the octets
53 written in the input. It is an error to specify a network address that
54 has bits set to the right of the specified netmask.
56 Table 8.22 shows some examples.
58 Table 8.22. cidr Type Input Examples
59 cidr Input cidr Output abbrev(cidr)
60 192.168.100.128/25 192.168.100.128/25 192.168.100.128/25
61 192.168/24 192.168.0.0/24 192.168.0/24
62 192.168/25 192.168.0.0/25 192.168.0.0/25
63 192.168.1 192.168.1.0/24 192.168.1/24
64 192.168 192.168.0.0/24 192.168.0/24
65 128.1 128.1.0.0/16 128.1/16
66 128 128.0.0.0/16 128.0/16
67 128.1.2 128.1.2.0/24 128.1.2/24
68 10.1.2 10.1.2.0/24 10.1.2/24
69 10.1 10.1.0.0/16 10.1/16
71 10.1.2.3/32 10.1.2.3/32 10.1.2.3/32
72 2001:4f8:3:ba::/64 2001:4f8:3:ba::/64 2001:4f8:3:ba/64
73 2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128
74 2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128
75 2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128
76 ::ffff:1.2.3.0/120 ::ffff:1.2.3.0/120 ::ffff:1.2.3/120
77 ::ffff:1.2.3.0/128 ::ffff:1.2.3.0/128 ::ffff:1.2.3.0/128
79 8.9.3. inet vs. cidr #
81 The essential difference between inet and cidr data types is that inet
82 accepts values with nonzero bits to the right of the netmask, whereas
83 cidr does not. For example, 192.168.0.1/24 is valid for inet but not
88 If you do not like the output format for inet or cidr values, try the
89 functions host, text, and abbrev.
93 The macaddr type stores MAC addresses, known for example from Ethernet
94 card hardware addresses (although MAC addresses are used for other
95 purposes as well). Input is accepted in the following formats:
104 These examples all specify the same address. Upper and lower case is
105 accepted for the digits a through f. Output is always in the first of
108 IEEE Standard 802-2001 specifies the second form shown (with hyphens)
109 as the canonical form for MAC addresses, and specifies the first form
110 (with colons) as used with bit-reversed, MSB-first notation, so that
111 08-00-2b-01-02-03 = 10:00:D4:80:40:C0. This convention is widely
112 ignored nowadays, and it is relevant only for obsolete network
113 protocols (such as Token Ring). PostgreSQL makes no provisions for bit
114 reversal; all accepted formats use the canonical LSB order.
116 The remaining five input formats are not part of any standard.
120 The macaddr8 type stores MAC addresses in EUI-64 format, known for
121 example from Ethernet card hardware addresses (although MAC addresses
122 are used for other purposes as well). This type can accept both 6 and 8
123 byte length MAC addresses and stores them in 8 byte length format. MAC
124 addresses given in 6 byte format will be stored in 8 byte length format
125 with the 4th and 5th bytes set to FF and FE, respectively. Note that
126 IPv6 uses a modified EUI-64 format where the 7th bit should be set to
127 one after the conversion from EUI-48. The function macaddr8_set7bit is
128 provided to make this change. Generally speaking, any input which is
129 comprised of pairs of hex digits (on byte boundaries), optionally
130 separated consistently by one of ':', '-' or '.', is accepted. The
131 number of hex digits must be either 16 (8 bytes) or 12 (6 bytes).
132 Leading and trailing whitespace is ignored. The following are examples
133 of input formats that are accepted:
134 '08:00:2b:01:02:03:04:05'
135 '08-00-2b-01-02-03-04-05'
138 '0800.2b01.0203.0405'
139 '0800-2b01-0203-0405'
143 These examples all specify the same address. Upper and lower case is
144 accepted for the digits a through f. Output is always in the first of
147 The last six input formats shown above are not part of any standard.
149 To convert a traditional 48 bit MAC address in EUI-48 format to
150 modified EUI-64 format to be included as the host portion of an IPv6
151 address, use macaddr8_set7bit as shown:
152 SELECT macaddr8_set7bit('08:00:2b:01:02:03');
155 -------------------------
156 0a:00:2b:ff:fe:01:02:03