E
E
EvgMul2017-03-10 09:45:13
PostgreSQL
EvgMul, 2017-03-10 09:45:13

How to write a query in PostgreSQL that checks the range of ip addresses?

I have two fields of type inet. There is an ip address of the user. We need a query that will find a string that matches the range of ip addresses in the database.
I found in the documentation that it is possible to check for a range (if I understand correctly)
https://www.postgresql.org/docs/8.0/static/functio...
I'm not good with SQL and I can't figure out how to make a query for this.
Please tell me how to do it?
Thanks in advance to all who respond.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Falseclock, 2017-03-10
@EvgMul

For example, is the address 192.168.0.100 included in the network 192.168.0.0/24

postgres=# select  inet '192.168.0.100' <<= inet '192.168.0.0/24';
 ?column?
----------
 t
(1 row)

or
Is the IP address 192.168.2.112 in the range between 192.168.0.0 and 192.168.10.255
postgres=# select  inet '192.168.2.112' >= inet '192.168.0.0' AND inet '192.168.2.112' <= inet '192.168.10.255';
 ?column?
----------
 t
(1 row)

postgres=#

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question