Reference: Inet Type
The inet type stores an IPv4 or IPv6 host address, optionally together with its subnet as a netmask length.
For example, 192.168.1.5/24 describes the host 192.168.1.5 in the subnet 192.168.1.0/24.
inet functionality.
PostgreSQL’s network operators, functions (for example, <<, host(), or masklen()), the cidr type, and
non-standard prefix-only IPs (10/24) are not yet supported.Usage Example
create table connections (
client inet,
ts timestamp
);
insert into connections values
('192.168.1.226/24', now()),
('10.1.2.3', now()),
('10:23::f1/64', now()),
('::1', now());
select client from connections order by client; client
------------------
10.1.2.3
192.168.1.226/24
::1
10:23::f1/64
(4 rows)Input
inet values are written as address/bits, where address is an IPv4 or IPv6 address and bits is the netmask.
Omitting the /bits specifies a single host with a full netmask.
-- IPv4
select inet '192.168.1.226/24';
select inet '192.168.1.226';
-- IPv6
select inet '10:23::f1/64';
select inet '0000:0000:0000:0000:0000:0000:0000:0001';
-- IPv6 with the last 32 bits in dotted IPv4 notation
select inet '::4.3.2.1/24';Output
CedarDB prints inet values in their normalized RFC 3493 form, and omits the netmask in the output for a single hosts.
IPv6 addresses are printed in their shortest form, without leading zeros and with the longest run of zero groups
replaced by :::
select inet '10.1.2.3/32', inet '0000:0000:0000:0000:0000:0000:0000:0001/128', inet '8000:0000:0000:0000:0000:0000:0000:0000/1'; inet | inet | inet
----------+------+---------
10.1.2.3 | ::1 | 8000::/1
(1 row)Comparison and Sorting
inet values support the regular comparison operators:
IPv4 addresses sort before IPv6 addresses.
Within the same address family, values are first compared by their network part, then by their netmask length,
and then by the full address.
select a from (values
('192.168.1.0/24'::inet),
('192.168.1.0/25'::inet),
('192.168.1.1/23'::inet),
('10:23::ffff'::inet),
('10.1.2.3'::inet)
) as i(a) order by a; a
----------------
10.1.2.3
192.168.1.1/23
192.168.1.0/24
192.168.1.0/25
10:23::ffff
(5 rows)Functions
inet_client_addr
inet_client_addr() returns the IP address of the client connected to the current session, NULL for connections via
Unix domain socket.
select inet_client_addr(); inet_client_addr
------------------
192.168.1.42
(1 row)