GeoIP on PostgreSQL 9.5

This guide is for adding GeoIP functionality to PostgreSQL 9.5 (and earlier) using MaxMind’s GeoLite2 CSV files.

You can read about the MaxMind GeoIP2 CSV files, and download free versions. You’ll need the CSV files to continue.

I create two tables; geoip_blocks and geoip_locations to contain the data. I load the English country names, but you could choose the language you want when you populate the geoip_locations table. I have copied the relevant GeoLite2 CSV files into the /tmp directory in the example below, but you could read them from anywhere.

You can use the index support for cidr addresses built in to PostgreSQL 9.4 and later, but you get much better performance using ip4r instead.

I have examples of both approaches here: https://gist.github.com/karlvr/8ff1900bfc9cba36468640c26ae3b2bb

This was posted 8 years ago. It has 0 notes.