Hosting Postgres with GeoLite2: a practical guide to IP geolocation, data loading, and updates
Loading MaxMind's GeoLite2 CSVs into Postgres pays off over the MMDB binary format when you need SQL joins, batch geolocation of many IPs, queries over the ranges themselves, or one copy shared across services; for simple single-IP lookups MMDB is easier. Three deployment options are covered: a docker-entrypoint-initdb.d script that downloads and COPYs the data, a custom image with the data baked in, and a scheduled loader service for updates. Lookups use the cidr `>>=` operator backed by a GiST inet_ops index, which keeps queries under 10ms. Recommended practice is weekly refreshes to match MaxMind's Tuesday releases, loading into new tables and swapping them via renames in a transaction, with roughly 500-600 MB of storage for all three databases and a ~200 MB City index that should fit in shared_buffers.
Related posts
- How and when to use btree_gist
Neon mediumAdept
Postgres's btree_gist extension lets a single GiST index combine one-dimensional B-Tree-style columns (like dates) with spatial PostGIS geometries, so queries filtering on both space and time can use one index scan. Using 38.6 million UK police crime records loaded via xsv and COPY, then transformed from WGS84 to British National Grid, a count of crimes within 1km of Stratford during the 2012 Olympics takes about 8 seconds unindexed. A GiST spatial index cuts that to about 32ms via a bitmap scan. A combined gist(location, month) index brings it to about 8ms with a plain Index Scan, roughly 1000x faster than no index.
- How we migrated Clera’s 500 GB production database to ClickHouse Managed Postgres overnight
ClickHouse mediumAdept
Clera moved a 500 GB, 500+ table production Postgres database from the EU to the US overnight onto ClickHouse Managed Postgres, with production traffic still running, using two parallel ClickPipes pipelines. The vendor choice followed a replay of their real workload (hot reads, point lookups, background analytics) on Supabase, Neon, PlanetScale and ClickHouse, plus sysbench TPC-C at 8–128 threads. Local NVMe storage beat network-attached storage, with 16–42% higher TPS than PlanetScale out of the box, and CPU fell from days at 100% to 10–20%. The hard parts were extension portability, restoring triggers, rebuilding indexes, resequencing tables, and doing parity checks before cutover.
- IP-based Geolocation in ClickHouse
ClickHouse mediumAdept
ClickHouse's ip_trie dictionary layout enables low-latency IP-to-location lookups once IP ranges are expressed as CIDR blocks. The walkthrough loads the DB-IP city dataset via the URL table engine and converts start/end ranges to CIDR using bitXor, log2/ceil, bitNot and bitAnd. It then builds an in-memory ip_trie dictionary queried with dictGet against SEC EDGAR access logs. For mapping, geohashEncode at low precision with log10-normalized counts rolls 20M+ rows into about 6,300 cells for a Grafana Geo Map panel.
- Geo Queries with PostGIS in Ionic Angular
Supabase mediumAdeptPostGIS on Supabase handles geo data for an Ionic Angular app with a Capacitor Google Map. A stores table holds geography(POINT) locations behind a GIST spatial index. Two SQL functions do the geo queries: nearby_stores orders stores by the <-> distance operator and returns st_distance in meters, and stores_in_view filters with && against an ST_MakeBox2D bounding box. The app calls them via Supabase RPC, inserts locations as WKT POINT(long lat) strings, uploads store images to Supabase Storage, and covers the native location permissions and API keys needed for iOS and Android.
- Introducing chdb Postgres extension: High-performance imports from cloud storage
ClickHouse mediumAdept
chdb is a new Postgres extension that uses the in-process ClickHouse engine chDB to import and export data in dozens of formats (CSV, JSON, Parquet, Avro, Arrow, ORC, Protobuf and more) from S3, GCS, Azure, HDFS, HTTP and local files, via a chdb_query() function and a chdb_hook module that extends COPY and CREATE TABLE. Importing a 1M-row NYC Taxi dataset from S3, pg_duckdb and pg_lake took 2-3x longer than chdb, and only aws_s3, which supports far fewer formats, came close. Rather than linking chDB into the backend or running a persistent service, chdb starts a helper process on demand and streams ClickHouse Native-format results over file descriptors, then exits, so the Postgres server pays no lasting memory cost and is isolated from out-of-memory failures. Type mapping goes through pg-clickhouse-c, with a structure option to override mappings such as Postgres JSON to ClickHouse JSON.