Search⌘ K

Finding an IP Address in the Ranges

Explore how to locate an IP address within various IP ranges in PostgreSQL using the ip4r extension and GiST indexing. Understand the operators that facilitate fast and accurate geolocation queries and learn to apply these techniques with practical SQL commands.

Here’s what the main data looks like:

PostgreSQL
table geolite.blocks limit 10;

The table command is SQL standard, so we might as well use it:

       iprange       │ locid  
═════════════════════╪════════
 1.0.0.0/24          │ 617943
 1.0.1.0-1.0.3.255   │ 104084
 1.0.4.0/22          │     17
 1.0.8.0/21          │  47667
 1.0.16.0/20         │ 879228
 1.0.32.0/19         │  47667
 1.0.64.0-1.0.81.255 │ 885221
 1.0.82.0/24         │ 902132
 1.0.83.0-1.0.86.255 │ 885221
 1.0.87.0/24         │ 873145
(10 rows)

What we have here is an iprange column. We can ...