Kevin Burke
Kevin Burke

Reputation: 64756

Postgres "is not contained by" operator for IP addresses or inet types

Postgres has an operator for determining whether an IP address is contained by a given range, for example:

SELECT * FROM clients WHERE ip_address <<= inet '10.0.0.0/16';

How could I filter for the reverse set, the set of addresses not in that subnet? The documentation of net functions does not seem to have an operator that can do this. https://www.postgresql.org/docs/12/functions-net.html

Upvotes: 3

Views: 1827

Answers (1)

klin
klin

Reputation: 121474

Use NOT:

SELECT * 
FROM clients 
WHERE NOT (ip_address <<= inet '10.0.0.0/16');

Parentheses are not necessary, inet operators have higher precedence than boolean ones.

Upvotes: 4

Related Questions