Converting an int to an IP address
Is there an easy way to convert an int to an IP address in PostgreSQL? I was able to go from IP to int using this开发者_运维问答 code:
SELECT inet '1.2.3.4'-'0.0.0.0'
This doesn't work:
SELECT 16909060::inet
I didn't see anything in the documentation. Does anyone know how to do this?
SELECT '0.0.0.0'::inet + 16909060
In case anyone else is trying to select from a table containing IP addresses and the column is defined as a long
, you can cast to bigint
and the conversion will work.
select '0.0.0.0'::inet + cast(source_ip as bigint) from addresses;
精彩评论