Re: IP adrss and networks in Postgresql - Mailing list pgsql-general

From Tom Lane
Subject Re: IP adrss and networks in Postgresql
Date
Msg-id 14869.1076426496@sss.pgh.pa.us
Whole thread Raw
In response to IP adrss and networks in Postgresql  ("NTPT" <ntpt@centrum.cz>)
List pgsql-general
"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

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: newbie pl/pgsql question on trigger function error
Next
From: Andrew Sullivan
Date:
Subject: Re: I want to use postresql for this app, but...