liveBook · Manning

INET data type

As explained in Build Financial Software with Generative AI (From Scratch)

The PostgreSQL INET data type stores IPv4 or IPv6 host addresses and networks and supports network-aware operations.

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

How Build Financial Software with Generative AI (From Scratch) covers it

How deep it goes
Explains column selection, CIDR representation, validation, testing, and display formatting, but does not detail PostgreSQL internals.
Examples
Demonstrates an audit_log column, a subnet-containment query, `/32` normalization, invalid-address tests, and a subnet-stripping utility.
What it assumes
Assumes basic database tables and IP-address concepts while introducing CIDR notation and Pydantic’s network types through examples.
What you can do after
Diagnose/debug with it by validating address models, querying suspicious ranges, and correcting CIDR display for audit users.
How it connects
Connects INET with PostgreSQL, CIDR notation, audit logging, Pydantic, IPvAnyAddress, and IPvAnyNetwork.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Financial Software with Generative AI (From Scratch). The section links point to the parts of the book it draws on.