Sub topic 16
Network Address Types
Assume you know SELECT, CREATE TABLE, constraints, and basic PostgreSQL client use. This chapter focuses on choosing and querying PostgreSQL network address types.
PostgreSQL provides types for IP addresses and hardware addresses:
| Type | Stores | Typical use |
|---|---|---|
| `inet` | An IPv4 or IPv6 host address, optionally with a network mask | Client IP addresses and server addresses |
| `cidr` | An IPv4 or IPv6 network | Subnets and permitted address blocks |
| `macaddr` | A six-byte MAC address | Ethernet hardware addresses |
`inet` and `cidr`
An inet value can represent a single host or a network with a prefix length:
SELECT
'192.0.2.15'::inet AS host_address,
'192.0.2.15/24'::inet AS host_with_mask,
'192.0.2.0/24'::cidr AS network_block;The /24 suffix is the prefix length. It says that the first 24 bits identify the network. IPv6 uses the same notation, such as 2001:db8::/32.
Use inet when the value identifies a host. Use cidr when the value represents a network block. PostgreSQL validates both formats, so malformed addresses fail when inserted instead of remaining unchecked text.
CREATE TABLE access_rules (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
allowed_net cidr NOT NULL,
description text NOT NULL
);
INSERT INTO access_rules (allowed_net, description)
VALUES
('192.0.2.0/24', 'Documentation network'),
('2001:db8::/32', 'Documentation IPv6 network');
SELECT
allowed_net,
masklen(allowed_net) AS prefix_length,
abbrev(allowed_net) AS short_form
FROM access_rules;The containment operators are useful for access rules:
<<means strictly contained by.<<=means contained by, including equality.>>and>>=test the reverse relationship.&&tests whether two networks overlap.
For example, a request from 192.0.2.15 is inside 192.0.2.0/24:
SELECT
'192.0.2.15'::inet <<= '192.0.2.0/24'::cidr AS allowed,
'192.0.2.0/24'::cidr && '192.0.2.128/25'::cidr AS overlaps;PostgreSQL also provides host(), network(), and masklen() for extracting parts of an address.
MAC addresses
macaddr stores a six-byte Ethernet address. PostgreSQL accepts common colon-separated and hyphen-separated forms, then stores the value in its normalized form.
CREATE TABLE devices (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
ip_address inet NOT NULL,
mac_address macaddr,
CHECK (family(ip_address) IN (4, 6))
);
INSERT INTO devices (ip_address, mac_address)
VALUES ('192.0.2.15', '08:00:2b:01:02:03');
SELECT ip_address, mac_address
FROM devices;A MAC address identifies a network interface, not a user or a permanent device identity. Virtual machines, privacy features, and replacement hardware can change it. Use it only when that distinction matters.
Exercise
Exercise
Which type should store a rule allowing every address in the IPv6 network 2001:db8:1234::/48, and which operator checks whether 2001:db8:1234::25 is inside it?
Show answer
Use cidr for the network rule. Use <<= to test containment: '2001:db8:1234::25'::inet <<= '2001:db8:1234::/48'::cidr.