September 26, 2024: PostgreSQL 17 Released!
Supported Versions: Current (17) / 16 / 15 / 14 / 13 / 12
Development Versions: devel
Unsupported versions: 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

4.9. Network Address Type Functions

Table 4-18. cidr and inet Operators

Operator Description Usage
< Less than inet '192.168.1.5' < inet '192.168.1.6'
<= Less than or equal inet '192.168.1.5' <= inet '192.168.1.5'
= Equals inet '192.168.1.5' = inet '192.168.1.5'
>= Greater or equal inet '192.168.1.5' >= inet '192.168.1.5'
> Greater inet '192.168.1.5' > inet '192.168.1.4'
<> Not equal inet '192.168.1.5' <> inet '192.168.1.4'
<< is contained within inet '192.168.1.5' << inet '192.168.1/24'
<<= is contained within or equals inet '192.168.1/24' <<= inet '192.168.1/24'
>> contains inet'192.168.1/24' >> inet '192.168.1.5'
>>= contains or equals inet '192.168.1/24' >>= inet '192.168.1/24'

All of the operators for inet can be applied to cidr values as well. The operators <<, <<=, >>, >>= test for subnet inclusion: they consider only the network parts of the two addresses, ignoring any host part, and determine whether one network part is identical to or a subnet of the other.

Table 4-19. cidr and inet Functions

Function Returns Description Example Result
broadcast(inet) inet broadcast address for network broadcast('192.168.1.5/24') 192.168.1.255/24
host(inet) text extract IP address as text host('192.168.1.5/24') 192.168.1.5
masklen(inet) integer extract netmask length masklen('192.168.1.5/24') 24
netmask(inet) inet construct netmask for network netmask('192.168.1.5/24') 255.255.255.0
network(inet) cidr extract network part of address network('192.168.1.5/24') 192.168.1.0/24
text(inet) text extract IP address and masklen as text text(inet '192.168.1.5') 192.168.1.5/32
abbrev(inet) text extract abbreviated display as text abbrev(cidr '10.1.0.0/16') 10.1/16

All of the functions for inet can be applied to cidr values as well. The host(), text(), and abbrev() functions are primarily intended to offer alternative display formats.

Table 4-20. macaddr Functions

Function Returns Description Example Result
trunc(macaddr) macaddr set last 3 bytes to zero trunc(macaddr '12:34:56:78:90:ab') 12:34:56:00:00:00

The function trunc(macaddr) returns a MAC address with the last 3 bytes set to 0. This can be used to associate the remaining prefix with a manufacturer. The directory contrib/mac in the source distribution contains some utilities to create and maintain such an association table.

The macaddr type also supports the standard relational operators (>, <=, etc.) for lexicographical ordering.