Posts Tagged ‘


PostgreSQL and network operators

I am working on a couple of projects that require the use of PostgreSQL and I really like dealing with it. Full of features that are sometimes quite usefull. One of them is the <<= network operator.

is contained within or equals – inet ’192.168.1/24′ <<= inet ’192.168.1/24′

This way one can check if an ip is in the range of a given network. Pretty simple, yeah! But I had the need to query ip address that are not in a specific network range :) . Well there is no opposite operator – at least I couldn’t find one. So what I did is is the following:

SELECT COUNT(*) FROM ips WHERE  CASE WHEN ip <<= inet '192.168.1/24' THEN false ELSE true END