I want to check if an IP exists in a range of ranges, eg: SELECT * FROM ip_address WHERE ip IN (<list of ip ranges>)
Postgresql documentation states to use the << operator to check if an IP is contained within a single IP Range, eg: inet '192.168.1.5' << inet '192.168.1/24'
, but I'm not sure how to use it on a list of ranges without having to construct an OR chain of <<'s.
For IPv4, this pool is 32-bits (232) in size and contains 4,294,967,296 IPv4 addresses. The IPv6 address space is 128-bits (2128) in size, containing 340,282,366,920,938,463,463,374,607,431,768,211,456 IPv6 addresses. A bit is a digit in the binary numeral system, the basic unit for storing information.
inet. The inet type holds an IPv4 or IPv6 host address, and optionally its subnet, all in one field. The subnet is represented by the number of network address bits present in the host address (the “netmask”). If the netmask is 32 and the address is IPv4, then the value does not indicate a subnet, only a single host.
select inet '192.168.1.5' << any (array['192.168.1/24', '10/8']::inet[]);
?column?
----------
t
http://www.postgresql.org/docs/current/static/functions-comparisons.html#AEN18486
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With