PostgreSQL’s INET data type stores IP addresses and networks in a form that supports network-aware comparisons and functions. It is useful for audit logs when requests may need to be searched by address range rather than treated as plain text. section 9.3.1
Searching an address range
An audit query can find activity from a suspected subnet with `WHERE ip_address << '192.168.1.0/24'`. The same column can store a single address such as `192.168.1.25`. section 9.3.1
Listing 9.20 The audit_log table
CREATE TABLE audit_log ( #1
audit_log_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at TIMESTAMP NOT NULL DEFAULT NOW(), #2
user_id VARCHAR(25) DEFAULT NULL,
ip_address INET DEFAULT NULL, #3
user_agent VARCHAR(255) DEFAULT NULL,
http_request VARCHAR(10) DEFAULT NULL,
url VARCHAR(255) DEFAULT NULL,
message TEXT NOT NULL
);
Use the table definition to see INET applied to an audit_log table alongside request, user, timestamp, and message fields. section 9.3.1
INET values use CIDR notation, so a single address may appear with a `/32` suffix. That representation is technically correct but may be unsuitable for a user-facing audit screen. section 9.5.2
Account for the data type across storage, validation, and presentation:
- Choose INET when PostgreSQL network comparisons or range searches matter; a string column can store the text but lacks these specialized operations. section 9.3.1
- Model the value with Pydantic’s IPvAnyAddress when the record should represent one address, or IPvAnyNetwork when CIDR network behavior is intended. section 9.5.2
