"NTPT" <ntpt@centrum.cz> writes:
> I need to do some "IP address based guessing" what should happen inside my application.Ie if given ip adress is from
network 'foo' do something, if given IP adrss is from network 'bar', do something else.
> So I need to set up table with network definitions like this :
> create table network_rules (network inet, rules text )
Most likely you want to declare the network column as 'cidr' not 'inet',
so that the datatype will enforce that it's a valid network spec. I
can't imagine any case where you'd want bits set to the right of the
netmask in this table.
> Now i have given *some* ip adress and I need to create a SELECT that return all networks with given ip adress
belongsto , ordered by netmask, most accurate (ie more bits in netmask set ) first.
> Is it possible with postgres and inet datatype ?
Sure. The contains-or-equals operator selects the rows you want
(network >>= 'target-ip') and you can order by masklen(network).
regards, tom lane