Re: IP adrss and networks in Postgresql

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "NTPT" <ntpt(at)centrum(dot)cz>
Cc: "Pgsql-General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: IP adrss and networks in Postgresql
Date: 2004-02-10 15:21:36
Message-ID: 14869.1076426496@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"NTPT" <ntpt(at)centrum(dot)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 belongs to , 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-02-10 15:34:06 Re: number of rows in a cursor
Previous Message Tom Lane 2004-02-10 15:14:07 Re: newbie pl/pgsql question on trigger function error