Skip to content

geoip

IP-based geolocation query

geoip · IP-based geolocation query

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
1560geoipgeoip0.4.0GISBSD-2-ClauseSQL
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
----d--NoNoNoYesnono
Relationships
Schemasgeoip
Requiresip4r
See Alsopostgis_tiger_geocoder tzf nominatim_fdw pg_geohash

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY0.4.019 18 17 16 15geoipip4r
RPMPIGSTY0.4.019 18 17 16 15geoip_$vip4r_$v
DEBPIGSTY0.4.019 18 17 16 15postgresql-$v-geoippostgresql-$v-ip4r
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
el8.aarch64N/APIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
el9.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
el9.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
el10.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
el10.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
d12.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
d12.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
d13.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
d13.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
u22.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
u22.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
u24.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
u24.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
u26.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
u26.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0
PackageVersionOSORGSIZEFile URL
geoip_150.4.0el8.x86_64pigsty13.5 KiBgeoip_15-0.4.0-1.git20250804.fcad7f1PGSTY.el8.noarch.rpm
geoip_150.2.4el8.x86_64pgdg11.3 KiBgeoip_15-0.2.4-3.rhel8.noarch.rpm
geoip_150.4.0el8.aarch64pigsty13.5 KiBgeoip_15-0.4.0-1.git20250804.fcad7f1PGSTY.el8.noarch.rpm
geoip_150.2.4el8.aarch64pgdg11.3 KiBgeoip_15-0.2.4-3.rhel8.noarch.rpm
geoip_150.4.0el9.x86_64pigsty13.3 KiBgeoip_15-0.4.0-2PGSTY.el9.noarch.rpm
geoip_150.2.4el9.x86_64pgdg11.0 KiBgeoip_15-0.2.4-3.rhel9.noarch.rpm
geoip_150.4.0el9.aarch64pigsty13.3 KiBgeoip_15-0.4.0-2PGSTY.el9.noarch.rpm
geoip_150.2.4el9.aarch64pgdg10.8 KiBgeoip_15-0.2.4-3.rhel9.noarch.rpm
geoip_150.4.0el10.x86_64pigsty13.5 KiBgeoip_15-0.4.0-2PGSTY.el10.noarch.rpm
geoip_150.4.0el10.aarch64pigsty13.5 KiBgeoip_15-0.4.0-2PGSTY.el10.noarch.rpm
postgresql-15-geoip0.4.0d12.x86_64pigsty6.8 KiBpostgresql-15-geoip_0.4.0-2PGSTY~bookworm_all.deb
postgresql-15-geoip0.4.0d12.aarch64pigsty6.8 KiBpostgresql-15-geoip_0.4.0-2PGSTY~bookworm_all.deb
postgresql-15-geoip0.4.0d13.x86_64pigsty6.8 KiBpostgresql-15-geoip_0.4.0-2PGSTY~trixie_all.deb
postgresql-15-geoip0.4.0d13.aarch64pigsty6.8 KiBpostgresql-15-geoip_0.4.0-2PGSTY~trixie_all.deb
postgresql-15-geoip0.4.0u22.x86_64pigsty6.9 KiBpostgresql-15-geoip_0.4.0-2PGSTY~jammy_all.deb
postgresql-15-geoip0.4.0u22.aarch64pigsty6.9 KiBpostgresql-15-geoip_0.4.0-2PGSTY~jammy_all.deb
postgresql-15-geoip0.4.0u24.x86_64pigsty6.9 KiBpostgresql-15-geoip_0.4.0-2PGSTY~noble_all.deb
postgresql-15-geoip0.4.0u24.aarch64pigsty6.9 KiBpostgresql-15-geoip_0.4.0-2PGSTY~noble_all.deb
postgresql-15-geoip0.4.0u26.x86_64pigsty6.9 KiBpostgresql-15-geoip_0.4.0-2PGSTY~resolute_all.deb
postgresql-15-geoip0.4.0u26.aarch64pigsty6.9 KiBpostgresql-15-geoip_0.4.0-2PGSTY~resolute_all.deb

Source

pig build pkg geoip;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

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

Install this extension with pig:

pig install geoip;		# install by package name

pig install geoip -v 19;   # install for PG 19
pig install geoip -v 18;   # install for PG 18
pig install geoip -v 17;   # install for PG 17
pig install geoip -v 16;   # install for PG 16
pig install geoip -v 15;   # install for PG 15

Create this extension with:

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.

Table reader