Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an int to an IP address

Tags:

postgresql

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?

like image 324
User1 Avatar asked May 21 '10 23:05

User1


People also ask

Is an IP address an integer?

Well, remember that an IP address is a 32 or 128 bit unsigned integer and storing the IP as an integer can have many advantages which will facilitate analysis: Integers use less space: If you are using a database, the 32bit INT takes up 4 bytes whereas you will need 15bytes to store an IP address as a string.

Can you convert a URL to an IP address?

To do this in Chrome, simply open up the DevTools, navigate to the Network tab and select the site's HTML doc. You should then see the IP address associated with that URL under Headers > General > Remote Address.

What converts name to IP addresses?

DNS servers convert URLs and domain names into IP addresses that computers can understand and use. They translate what a user types into a browser into something the machine can use to find a webpage.


1 Answers

SELECT '0.0.0.0'::inet + 16909060
like image 79
Quassnoi Avatar answered Oct 12 '22 09:10

Quassnoi