user2315
user2315

Reputation: 831

Convert IP address in PostgreSQL to integer?

Is there a query that would be able to accomplish this?

For example given an entry '216.55.82.34' ..I would want to split the string by the '.'s, and apply the equation:

IP Number = 16777216*w + 65536*x + 256*y + z where IP Address = w.x.y.z

Would this be possible from just a Query?

Upvotes: 10

Views: 16856

Answers (5)

Rui Lima
Rui Lima

Reputation: 7403

Consider changing the column data type to inet, maybe is more efficient.

ALTER TABLE iptable ALTER COLUMN ip_from TYPE inet
USING '0.0.0.0'::inet + ip_from::bigint;

create index on iptable using gist (ip_from inet_ops);

Then to query

SELECT ip_from
FROM iptable 
WHERE ip_from = '177.99.194.234'::inet

Upvotes: 0

Eric Twilegar
Eric Twilegar

Reputation: 607

create function dbo.fn_ipv4_to_int( p_ip text)
returns int
as $func$ 
select cast(case when cast( split_part(p_ip, '.', 1 ) as int ) >= 128
then 
(
( 256 - cast(split_part(p_ip, '.', 1 ) as int ))
* 
-power ( 2, 24 ) 
)
+ (cast( split_part(p_ip, '.', 2 ) as int ) * 65536 )
+ (cast( split_part(p_ip, '.', 3 ) as int ) * 256 )
+ (cast( split_part(p_ip, '.', 4 ) as int )  )

else (cast(split_part(p_ip, '.', 1 ) as int) * 16777216)
+ (cast(split_part(p_ip, '.', 2 ) as int) * 65536)
+ (cast(split_part(p_ip, '.', 3 ) as int) * 256)
+ (cast(split_part(p_ip, '.', 4 ) as int))
end as int )
$func$ LANGUAGE SQL  IMMUTABLE RETURNS NULL ON NULL INPUT;

in case you need to get a 32 bit int. it'll return negative numbers for ips over 128.0.0.0. I'd use bigint if you can, but i had a case when i had the numbers stored as 32 bit numbers from another database.

Upvotes: 0

mad
mad

Reputation: 902

PG 9.4

create or replace function ip2numeric(ip varchar) returns numeric AS
$$
DECLARE
  ip_numeric numeric;
BEGIN
  EXECUTE format('SELECT inet %L - %L', ip, '0.0.0.0') into ip_numeric;

  return ip_numeric;
END;
$$ LANGUAGE plpgsql;

Usage

select ip2numeric('192.168.1.2');
$ 3232235778

Upvotes: 1

pumbo
pumbo

Reputation: 3826

You can simply convert inet data-type to bigint: (inet_column - '0.0.0.0'::inet)

For example:

SELECT ('127.0.0.1'::inet - '0.0.0.0'::inet) as ip_integer

will output 2130706433, which is the integer representation of IP address 127.0.0.1

Upvotes: 35

leonbloy
leonbloy

Reputation: 75896

You can use split_part(). For example:

CREATE FUNCTION ip2int(text) RETURNS bigint AS $$ 
SELECT split_part($1,'.',1)::bigint*16777216 + split_part($1,'.',2)::bigint*65536 +
 split_part($1,'.',3)::bigint*256 + split_part($1,'.',4)::bigint;
$$ LANGUAGE SQL  IMMUTABLE RETURNS NULL ON NULL INPUT;


SELECT ip2int('200.233.1.2');
>> 3370713346

Or, if don't want to define a function, simply :

SELECT split_part(ip,'.',1)::bigint*16777216 + split_part(ip,'.',2)::bigint*65536 +
 split_part(ip,'.',3)::bigint*256 + split_part(ip,'.',4)::bigint;

The drawback of the later is that, if the value is given by some computation instead of being just a table field, it can be inefficient to compute, or ugly to write.

Upvotes: 12

Related Questions