跳转到主要内容

geoip

IP 地理位置扩展(围绕 MaxMind GeoLite 数据集的包装器)

geoip · IP 地理位置扩展(围绕 MaxMind GeoLite 数据集的包装器)

概览

ID扩展软件包版本类别许可证编程语言
1560geoipgeoip0.4.0GISBSD-2-ClauseSQL
属性可执行文件动态库需要预加载需要建扩展可迁移可信
----d--否否否是否否
关联
模式geoip
依赖ip4r
相关扩展postgis_tiger_geocoder tzf nominatim_fdw pg_geohash

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
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
软件包版本平台仓库大小下载
geoip_190.4.0el9.x86_64pigsty13.3 KiBgeoip_19-0.4.0-2PGSTY.el9.noarch.rpm
geoip_190.4.0el9.aarch64pigsty13.3 KiBgeoip_19-0.4.0-2PGSTY.el9.noarch.rpm
geoip_190.4.0el10.x86_64pigsty13.5 KiBgeoip_19-0.4.0-2PGSTY.el10.noarch.rpm
geoip_190.4.0el10.aarch64pigsty13.5 KiBgeoip_19-0.4.0-2PGSTY.el10.noarch.rpm
postgresql-19-geoip0.4.0d12.x86_64pigsty6.8 KiBpostgresql-19-geoip_0.4.0-2PGSTY~bookworm_all.deb
postgresql-19-geoip0.4.0d12.aarch64pigsty6.8 KiBpostgresql-19-geoip_0.4.0-2PGSTY~bookworm_all.deb
postgresql-19-geoip0.4.0d13.x86_64pigsty6.8 KiBpostgresql-19-geoip_0.4.0-2PGSTY~trixie_all.deb
postgresql-19-geoip0.4.0d13.aarch64pigsty6.8 KiBpostgresql-19-geoip_0.4.0-2PGSTY~trixie_all.deb
postgresql-19-geoip0.4.0u22.x86_64pigsty6.9 KiBpostgresql-19-geoip_0.4.0-2PGSTY~jammy_all.deb
postgresql-19-geoip0.4.0u22.aarch64pigsty6.9 KiBpostgresql-19-geoip_0.4.0-2PGSTY~jammy_all.deb
postgresql-19-geoip0.4.0u24.x86_64pigsty6.9 KiBpostgresql-19-geoip_0.4.0-2PGSTY~noble_all.deb
postgresql-19-geoip0.4.0u24.aarch64pigsty6.9 KiBpostgresql-19-geoip_0.4.0-2PGSTY~noble_all.deb
postgresql-19-geoip0.4.0u26.x86_64pigsty6.9 KiBpostgresql-19-geoip_0.4.0-2PGSTY~resolute_all.deb
postgresql-19-geoip0.4.0u26.aarch64pigsty6.9 KiBpostgresql-19-geoip_0.4.0-2PGSTY~resolute_all.deb
软件包版本平台仓库大小下载
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

源码

geoip-0.4.0+git20250804.fcad7f1.tar.gz

pig build pkg geoip;		# build rpm/deb

安装

启用 PGDG 和 PIGSTY 仓库并更新缓存。

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

使用 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 EXTENSION geoip CASCADE; -- requires ip4r

用法

geoip: 基于 IP 的 PostgreSQL 地理定位

该扩展提供基于 IP 的地理定位——你提供一个 IPv4 或 IPv6 地址,扩展会查找国家、城市、GPS 坐标、ASN 等信息。它需要 ip4r 扩展和来自 MaxMind 的 GeoLite2 数据。

CREATE EXTENSION ip4r;
CREATE EXTENSION geoip;

函数

函数说明
geoip_country_code(ip4|ip6)返回国家代码(2 字符)
geoip_country(ip4|ip6)返回所有国家信息(代码、名称、网络)
geoip_city_location(ip4|ip6)仅返回位置 ID(INT)
geoip_city(ip4|ip6)返回所有城市信息(GPS、邮编等)
geoip_asn(ip4|ip6)返回 ASN 名称和 IP 范围

示例

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.

加载数据

该扩展需要来自 MaxMind 的 GeoLite2 CSV 数据。从 MaxMind GeoLite2 下载 City、Country 和 ASN 数据集的 CSV 格式,然后加载:

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)'

“locations” 文件有多种语言版本——选择适合你的即可。

表格阅读器