↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

Wiki / 数据类型

inet

IPv4 or IPv6 host address

当前阅读 PG 18·选择有来源记录的版本

此版本暂无所选语言的定义,以下显示原始英文内容。

Catalog name
pg_catalog.inet
Type OID
869
Type kind
Base type
Declared length
Variable length (varlena)
Storage strategy
main
Input function
inet_in
Output function
inet_out
Documented declaration
inet
Storage Size
7 or 19 bytes
Manual description
IPv4 and IPv6 hosts and networks
aliases
未知
coverage
source inventory; exact declared input types for operator classes
manual documentation
dedicated family chapter
manual path
datatype-net-types.html
ranges
未知
signature
inet

版本定义 PG 18

8.9. Network Address Types

PostgreSQL offers data types to store IPv4, IPv6, and MAC addresses, as shown in Table 8.21. It is better to use these types instead of plain text types to store network addresses, because these types offer input error checking and specialized operators and functions (see Section 9.12).

Table 8.21. Network Address Types

Name Storage Size Description
cidr 7 or 19 bytes IPv4 and IPv6 networks
inet 7 or 19 bytes IPv4 and IPv6 hosts and networks
macaddr 6 bytes MAC addresses
macaddr8 8 bytes MAC addresses (EUI-64 format)

When sorting inet or cidr data types, IPv4 addresses will always sort before IPv6 addresses, including IPv4 addresses encapsulated or mapped to IPv6 addresses, such as ::10.2.3.4 or ::ffff:10.4.3.2.

8.9.1. inet

The inet type holds an IPv4 or IPv6 host address, and optionally its subnet, all in one field. The subnet is represented by the number of network address bits present in the host address (the “netmask”). If the netmask is 32 and the address is IPv4, then the value does not indicate a subnet, only a single host. In IPv6, the address length is 128 bits, so 128 bits specify a unique host address. Note that if you want to accept only networks, you should use the cidr type rather than inet.

The input format for this type is address/y where address is an IPv4 or IPv6 address and y is the number of bits in the netmask. If the /y portion is omitted, the netmask is taken to be 32 for IPv4 or 128 for IPv6, so the value represents just a single host. On display, the /y portion is suppressed if the netmask specifies a single host.

8.9.2. cidr

The cidr type holds an IPv4 or IPv6 network specification. Input and output formats follow Classless Internet Domain Routing conventions. The format for specifying networks is address/y where address is the network's lowest address represented as an IPv4 or IPv6 address, and y is the number of bits in the netmask. If y is omitted, it is calculated using assumptions from the older classful network numbering system, except it will be at least large enough to include all of the octets written in the input. It is an error to specify a network address that has bits set to the right of the specified netmask.

Table 8.22 shows some examples.

Table 8.22. cidr Type Input Examples

cidr Input cidr Output abbrev(cidr)
192.168.100.128/25 192.168.100.128/25 192.168.100.128/25
192.168/24 192.168.0.0/24 192.168.0/24
192.168/25 192.168.0.0/25 192.168.0.0/25
192.168.1 192.168.1.0/24 192.168.1/24
192.168 192.168.0.0/24 192.168.0/24
128.1 128.1.0.0/16 128.1/16
128 128.0.0.0/16 128.0/16
128.1.2 128.1.2.0/24 128.1.2/24
10.1.2 10.1.2.0/24 10.1.2/24
10.1 10.1.0.0/16 10.1/16
10 10.0.0.0/8 10/8
10.1.2.3/32 10.1.2.3/32 10.1.2.3/32
2001:4f8:3:ba::/64 2001:4f8:3:ba::/64 2001:4f8:3:ba/64
2001:4f8:3:ba:​2e0:81ff:fe22:d1f1/128 2001:4f8:3:ba:​2e0:81ff:fe22:d1f1/128 2001:4f8:3:ba:​2e0:81ff:fe22:d1f1/128
::ffff:1.2.3.0/120 ::ffff:1.2.3.0/120 ::ffff:1.2.3/120
::ffff:1.2.3.0/128 ::ffff:1.2.3.0/128 ::ffff:1.2.3.0/128

8.9.3. inet vs. cidr

The essential difference between inet and cidr data types is that inet accepts values with nonzero bits to the right of the netmask, whereas cidr does not. For example, 192.168.0.1/24 is valid for inet but not for cidr.

Tip

If you do not like the output format for inet or cidr values, try the functions host, text, and abbrev.

8.9.4. macaddr

The macaddr type stores MAC addresses, known for example from Ethernet card hardware addresses (although MAC addresses are used for other purposes as well). Input is accepted in the following formats:

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

These examples all specify the same address. Upper and lower case is accepted for the digits a through f. Output is always in the first of the forms shown.

IEEE Standard 802-2001 specifies the second form shown (with hyphens) as the canonical form for MAC addresses, and specifies the first form (with colons) as used with bit-reversed, MSB-first notation, so that 08-00-2b-01-02-03 = 10:00:D4:80:40:C0. This convention is widely ignored nowadays, and it is relevant only for obsolete network protocols (such as Token Ring). PostgreSQL makes no provisions for bit reversal; all accepted formats use the canonical LSB order.

The remaining five input formats are not part of any standard.

8.9.5. macaddr8

The macaddr8 type stores MAC addresses in EUI-64 format, known for example from Ethernet card hardware addresses (although MAC addresses are used for other purposes as well). This type can accept both 6 and 8 byte length MAC addresses and stores them in 8 byte length format. MAC addresses given in 6 byte format will be stored in 8 byte length format with the 4th and 5th bytes set to FF and FE, respectively. Note that IPv6 uses a modified EUI-64 format where the 7th bit should be set to one after the conversion from EUI-48. The function macaddr8_set7bit is provided to make this change. Generally speaking, any input which is comprised of pairs of hex digits (on byte boundaries), optionally separated consistently by one of ':', '-' or '.', is accepted. The number of hex digits must be either 16 (8 bytes) or 12 (6 bytes). Leading and trailing whitespace is ignored. The following are examples of input formats that are accepted:

'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'

These examples all specify the same address. Upper and lower case is accepted for the digits a through f. Output is always in the first of the forms shown.

The last six input formats shown above are not part of any standard.

To convert a traditional 48 bit MAC address in EUI-48 format to modified EUI-64 format to be included as the host portion of an IPv6 address, use macaddr8_set7bit as shown:

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

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

比较版本

完整来源事实

casts

map[castcontext:i castfunc:0 castmethod:b castsource:cidr casttarget:inet], map[castcontext:a castfunc:cidr castmethod:f castsource:inet casttarget:cidr], map[castcontext:a castfunc:text(inet) castmethod:f castsource:inet casttarget:text], map[castcontext:a castfunc:text(inet) castmethod:f castsource:inet casttarget:varchar], map[castcontext:a castfunc:text(inet) castmethod:f castsource:inet casttarget:bpchar]

catalog

{"array_type_name":"_inet","array_type_oid":"1041","descr":"IP address/netmask, host address, netmask optional","oid":"869","typacl":"_null_","typalign":"i","typanalyze":"-","typarray":"0","typbasetype":"0","typbyval":"f","typcategory":"I","typcollation":"0","typdefault":"_null_","typdefaultbin":"_null_","typdelim":"','","typelem":"0","typinput":"inet_in","typisdefined":"t","typispreferred":"t","typlen":"-1","typmodin":"-","typmodout":"-","typname":"inet","typnamespace":"pg_catalog","typndims":"0","typnotnull":"f","typoutput":"inet_out","typowner":"POSTGRES","typreceive":"inet_recv","typrelid":"0","typsend":"inet_send","typstorage":"m","typsubscript":"-","typtype":"b","typtypmod":"-1"}

operator classes

map[opcdefault:f opcfamily:btree/network_ops opcintype:inet opckeytype:0 opcmethod:btree opcname:cidr_ops opcnamespace:pg_catalog opcowner:POSTGRES], map[opcdefault:f opcfamily:hash/network_ops opcintype:inet opckeytype:0 opcmethod:hash opcname:cidr_ops opcnamespace:pg_catalog opcowner:POSTGRES], map[opcdefault:t opcfamily:btree/network_ops opcintype:inet opckeytype:0 opcmethod:btree opcname:inet_ops opcnamespace:pg_catalog opcowner:POSTGRES], map[opcdefault:t opcfamily:hash/network_ops opcintype:inet opckeytype:0 opcmethod:hash opcname:inet_ops opcnamespace:pg_catalog opcowner:POSTGRES], map[opcdefault:f opcfamily:gist/network_ops opcintype:inet opckeytype:0 opcmethod:gist opcname:inet_ops opcnamespace:pg_catalog opcowner:POSTGRES], map[opcdefault:t opcfamily:spgist/network_ops opcintype:inet opckeytype:0 opcmethod:spgist opcname:inet_ops opcnamespace:pg_catalog opcowner:POSTGRES], map[opcdefault:f opcfamily:brin/network_minmax_ops opcintype:inet opckeytype:inet opcmethod:brin opcname:inet_minmax_ops opcnamespace:pg_catalog opcowner:POSTGRES], map[opcdefault:f opcfamily:brin/network_minmax_multi_ops opcintype:inet opckeytype:inet opcmethod:brin opcname:inet_minmax_multi_ops opcnamespace:pg_catalog opcowner:POSTGRES], map[opcdefault:f opcfamily:brin/network_bloom_ops opcintype:inet opckeytype:inet opcmethod:brin opcname:inet_bloom_ops opcnamespace:pg_catalog opcowner:POSTGRES], map[opcdefault:t opcfamily:brin/network_inclusion_ops opcintype:inet opckeytype:inet opcmethod:brin opcname:inet_inclusion_ops opcnamespace:pg_catalog opcowner:POSTGRES]

operators

map[descr:equal oid:1201 oprcanhash:t oprcanmerge:t oprcode:network_eq oprcom:=(inet,inet) oprjoin:eqjoinsel oprkind:b oprleft:inet oprname:= oprnamespace:pg_catalog oprnegate:<>(inet,inet) oprowner:POSTGRES oprrest:eqsel oprresult:bool oprright:inet], map[descr:not equal oid:1202 oprcanhash:f oprcanmerge:f oprcode:network_ne oprcom:<>(inet,inet) oprjoin:neqjoinsel oprkind:b oprleft:inet oprname:<> oprnamespace:pg_catalog oprnegate:=(inet,inet) oprowner:POSTGRES oprrest:neqsel oprresult:bool oprright:inet], map[descr:less than oid:1203 oprcanhash:f oprcanmerge:f oprcode:network_lt oprcom:>(inet,inet) oprjoin:scalarltjoinsel oprkind:b oprleft:inet oprname:< oprnamespace:pg_catalog oprnegate:>=(inet,inet) oprowner:POSTGRES oprrest:scalarltsel oprresult:bool oprright:inet], map[descr:less than or equal oid:1204 oprcanhash:f oprcanmerge:f oprcode:network_le oprcom:>=(inet,inet) oprjoin:scalarlejoinsel oprkind:b oprleft:inet oprname:<= oprnamespace:pg_catalog oprnegate:>(inet,inet) oprowner:POSTGRES oprrest:scalarlesel oprresult:bool oprright:inet], map[descr:greater than oid:1205 oprcanhash:f oprcanmerge:f oprcode:network_gt oprcom:<(inet,inet) oprjoin:scalargtjoinsel oprkind:b oprleft:inet oprname:> oprnamespace:pg_catalog oprnegate:<=(inet,inet) oprowner:POSTGRES oprrest:scalargtsel oprresult:bool oprright:inet], map[descr:greater than or equal oid:1206 oprcanhash:f oprcanmerge:f oprcode:network_ge oprcom:<=(inet,inet) oprjoin:scalargejoinsel oprkind:b oprleft:inet oprname:>= oprnamespace:pg_catalog oprnegate:<(inet,inet) oprowner:POSTGRES oprrest:scalargesel oprresult:bool oprright:inet], map[descr:is subnet oid:931 oid_symbol:OID_INET_SUB_OP oprcanhash:f oprcanmerge:f oprcode:network_sub oprcom:>>(inet,inet) oprjoin:networkjoinsel oprkind:b oprleft:inet oprname:<< oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:networksel oprresult:bool oprright:inet], map[descr:is subnet or equal oid:932 oid_symbol:OID_INET_SUBEQ_OP oprcanhash:f oprcanmerge:f oprcode:network_subeq oprcom:>>=(inet,inet) oprjoin:networkjoinsel oprkind:b oprleft:inet oprname:<<= oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:networksel oprresult:bool oprright:inet], map[descr:is supernet oid:933 oid_symbol:OID_INET_SUP_OP oprcanhash:f oprcanmerge:f oprcode:network_sup oprcom:<<(inet,inet) oprjoin:networkjoinsel oprkind:b oprleft:inet oprname:>> oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:networksel oprresult:bool oprright:inet], map[descr:is supernet or equal oid:934 oid_symbol:OID_INET_SUPEQ_OP oprcanhash:f oprcanmerge:f oprcode:network_supeq oprcom:<<=(inet,inet) oprjoin:networkjoinsel oprkind:b oprleft:inet oprname:>>= oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:networksel oprresult:bool oprright:inet], map[descr:overlaps (is subnet or supernet) oid:3552 oid_symbol:OID_INET_OVERLAP_OP oprcanhash:f oprcanmerge:f oprcode:network_overlap oprcom:&&(inet,inet) oprjoin:networkjoinsel oprkind:b oprleft:inet oprname:&& oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:networksel oprresult:bool oprright:inet], map[descr:bitwise not oid:2634 oprcanhash:f oprcanmerge:f oprcode:inetnot oprcom:0 oprjoin:- oprkind:l oprleft:0 oprname:~ oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:inet oprright:inet], map[descr:bitwise and oid:2635 oprcanhash:f oprcanmerge:f oprcode:inetand oprcom:0 oprjoin:- oprkind:b oprleft:inet oprname:& oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:inet oprright:inet], map[descr:bitwise or oid:2636 oprcanhash:f oprcanmerge:f oprcode:inetor oprcom:0 oprjoin:- oprkind:b oprleft:inet oprname:| oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:inet oprright:inet], map[descr:add oid:2637 oprcanhash:f oprcanmerge:f oprcode:inetpl oprcom:+(int8,inet) oprjoin:- oprkind:b oprleft:inet oprname:+ oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:inet oprright:int8], map[descr:add oid:2638 oprcanhash:f oprcanmerge:f oprcode:int8pl_inet oprcom:+(inet,int8) oprjoin:- oprkind:b oprleft:int8 oprname:+ oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:inet oprright:inet], map[descr:subtract oid:2639 oprcanhash:f oprcanmerge:f oprcode:inetmi_int8 oprcom:0 oprjoin:- oprkind:b oprleft:inet oprname:- oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:inet oprright:int8], map[descr:subtract oid:2640 oprcanhash:f oprcanmerge:f oprcode:inetmi oprcom:0 oprjoin:- oprkind:b oprleft:inet oprname:- oprnamespace:pg_catalog oprnegate:0 oprowner:POSTGRES oprrest:- oprresult:int8 oprright:inet]

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · 555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f

正文语言: en · 555610c24d53e4316da5b7d3fc25c279d96856d5e0e23ee308c328c5fa881d9f