geoip

IP-based geolocation query

Overview

PackageVersionCategoryLicenseLanguage
geoip0.3.0GISBSD 2-ClauseSQL
IDExtensionBinLibLoadCreateTrustRelocSchema
1560geoipNoYesNoYesNoNogeoip
Relatedip4r postgis tzf country postgis_topology postgis_raster postgis_sfcgal postgis_tiger_geocoder address_standardizer

no pg17 on el9, no legacy branch on el8

Version

TypeRepoVersionPG VerPackageDeps
EXTMIXED0.3.01817161514geoipip4r
RPMPIGSTY0.3.01817161514geoip_$v-
DEBPIGSTY0.3.01817161514postgresql-$v-geoip-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
u22.x86_64
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
u22.aarch64
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
u24.x86_64
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
u24.aarch64
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0
PIGSTY 0.3.0

Build

You can build the RPM / DEB packages for geoip using pig build:

pig build pkg geoip         # build RPM / DEB packages

Install

You can install geoip directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install geoip;          # Install for current active PG version
pig ext install -y geoip -v 18  # PG 18
pig ext install -y geoip -v 17  # PG 17
pig ext install -y geoip -v 16  # PG 16
pig ext install -y geoip -v 15  # PG 15
pig ext install -y geoip -v 14  # PG 14
dnf install -y geoip_18       # PG 18
dnf install -y geoip_17       # PG 17
dnf install -y geoip_16       # PG 16
dnf install -y geoip_15       # PG 15
dnf install -y geoip_14       # PG 14
apt install -y postgresql-18-geoip   # PG 18
apt install -y postgresql-17-geoip   # PG 17
apt install -y postgresql-16-geoip   # PG 16
apt install -y postgresql-15-geoip   # PG 15
apt install -y postgresql-14-geoip   # PG 14

Create Extension:

CREATE EXTENSION geoip CASCADE;  -- requires: ip4r

Usage

geoip: IP-based geolocation for PostgreSQL

This extension provides IP-based geolocation — you provide an IPv4 or IPv6 address and the extension looks up country, city, GPS coordinates, ASN and more. It requires the ip4r extension and GeoLite2 data from MaxMind.

CREATE EXTENSION ip4r;
CREATE EXTENSION geoip;

Functions

FunctionDescription
geoip_country_code(ip4|ip6)Returns country code (2 chars)
geoip_country(ip4|ip6)Returns all country info (code, name, network)
geoip_city_location(ip4|ip6)Returns just the location ID (INT)
geoip_city(ip4|ip6)Returns all city info (GPS, ZIP code, etc.)
geoip_asn(ip4|ip6)Returns ASN name and IP range

Examples

SELECT geoip_country_code('78.45.133.255'::ip4);
-- CZ

SELECT * FROM geoip.geoip_city('78.45.133.255'::ip4);
--  geoname_id | country_iso_code | city_name | postal_code | ...
-- ------------+------------------+-----------+-------------+----
--     3066399 | CZ               | Sardice   | 696 13      | ...

SELECT * FROM geoip.geoip_country('78.45.133.255'::ip4);
--     network     | country_iso_code | country_name
-- ----------------+------------------+--------------
--  78.45.128.0/17 | CZ               | Czechia

SELECT * FROM geoip.geoip_asn('78.45.133.255'::ip4);
--    network    | asn_number |      asn_name
-- --------------+------------+---------------------
--  78.44.0.0/15 |       6830 | Liberty Global B.V.

Loading Data

The extension requires GeoLite2 CSV data from MaxMind. Download the City, Country, and ASN datasets in CSV format from MaxMind GeoLite2, then load:

cat GeoLite2-Country-Locations-en.csv | \
  psql $DBNAME -c 'COPY geoip.geoip_country_locations FROM stdin WITH (FORMAT CSV, HEADER)'

cat GeoLite2-Country-Blocks-IPv4.csv | \
  psql $DBNAME -c 'COPY geoip.geoip_country_blocks FROM stdin WITH (FORMAT CSV, HEADER)'

cat GeoLite2-Country-Blocks-IPv6.csv | \
  psql $DBNAME -c 'COPY geoip.geoip_country_blocks FROM stdin WITH (FORMAT CSV, HEADER)'

cat GeoLite2-City-Locations-en.csv | \
  psql $DBNAME -c 'COPY geoip.geoip_city_locations FROM stdin WITH (FORMAT CSV, HEADER)'

cat GeoLite2-City-Blocks-IPv4.csv | \
  psql $DBNAME -c 'COPY geoip.geoip_city_blocks FROM stdin WITH (FORMAT CSV, HEADER)'

cat GeoLite2-City-Blocks-IPv6.csv | \
  psql $DBNAME -c 'COPY geoip.geoip_city_blocks FROM stdin WITH (FORMAT CSV, HEADER)'

cat GeoLite2-ASN-Blocks-IPv4.csv | \
  psql $DBNAME -c 'COPY geoip.geoip_asn_blocks FROM stdin WITH (FORMAT CSV, HEADER)'

cat GeoLite2-ASN-Blocks-IPv6.csv | \
  psql $DBNAME -c 'COPY geoip.geoip_asn_blocks FROM stdin WITH (FORMAT CSV, HEADER)'

The “locations” files have multiple language variants — pick the one that works for you.


Last Modified 2026-03-12: add pg extension catalog (95749bf)