10

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?

1
  • 4
    This is the thread people will want to find when they need to convert MySQL's INET_ATON() function to Postgres. Aug 3, 2017 at 7:45

5 Answers 5

35

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

2
  • 5
    This doesn't work for IPv6 because it goes out of range of the int type.
    – LtWorf
    Sep 23, 2016 at 7:37
  • @LtWorf Specifically it exceeds the range of a bigint, which is a 64-bit signed type, whereas IPv6 would need 128-bit unsigned.
    – Inkling
    Jan 2, 2022 at 1:14
11

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.

1
  • How would I apply this given my column name is ip? A bit confused
    – user2315
    Mar 27, 2013 at 15:51
1

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
0
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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.