Convert int to IP address

Is there an easy way to convert int to IP address in PostgreSQL? I was able to switch from IP to int using this code:

 SELECT inet '1.2.3.4' - '0.0.0.0'

This does not work:

 SELECT 16909060 :: inet

I did not see anything in the documentation. Does anyone know how to do this?

+7
postgresql
source share
2 answers
SELECT '0.0.0.0'::inet + 16909060 
+11
source share

In case someone tries to select from a table containing IP addresses, and the column is defined as long , you can use bigint and the conversion will work.

 select '0.0.0.0'::inet + cast(source_ip as bigint) from addresses; 
0
source share

All Articles