Mac

August 17, 2009

How to convert a bigint into an inet in postgresql ?

Filed under: Uncategorized — mac @ 12:29 pm

IPv4 adresses are typically bigint data - they are 32-bits data usually written as 4 blocks of 8 bits for practical reasons.

My version of postgresl doesn’t know how to translate this bigint natively into an inet, so I’m using the following trick:

select
((3221226219 & x’ff000000′::bigint >> 24 ) || ‘.’ ||
(3221226219 & x’00ff0000′::bigint >> 16 ) || ‘.’ ||
(3221226219 & x’0000ff00′::bigint >> 8 ) || ‘.’ ||
(3221226219 & x’000000ff’::bigint))::inet

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google]

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

Powered by WordPress