Permalink
Browse files

Initial version.

  • Loading branch information...
0 parents commit 9668dd948efc9884ad69737be6f342188c88edc9 @tvondra committed Apr 1, 2012
Showing with 535 additions and 0 deletions.
  1. +65 −0 INSTALL
  2. +25 −0 LICENSE
  3. +41 −0 META.json
  4. +12 −0 Makefile
  5. +159 −0 README.md
  6. +33 −0 doc/geoip.md
  7. +5 −0 geoip.control
  8. +185 −0 sql/geoip--0.0.1.sql
  9. 0 sql/uninstall_geoip.sql
  10. +2 −0 test/expected/base.out
  11. +8 −0 test/sql/base.sql
65 INSTALL
@@ -0,0 +1,65 @@
+To build it, just do this:
+
+ make
+ make installcheck
+ make install
+
+If you encounter an error such as:
+
+ "Makefile", line 8: Need an operator
+
+You need to use GNU make, which may well be installed on your system as
+`gmake`:
+
+ gmake
+ gmake install
+ gmake installcheck
+
+If you encounter an error such as:
+
+ make: pg_config: Command not found
+
+Be sure that you have `pg_config` installed and in your path. If you used a
+package management system such as RPM to install PostgreSQL, be sure that the
+`-devel` package is also installed. If necessary tell the build process where
+to find it:
+
+ env PG_CONFIG=/path/to/pg_config make && make installcheck && make install
+
+And finally, if all that fails (and if you're on PostgreSQL 8.1 or lower, it
+likely will), copy the entire distribution directory to the `contrib/`
+subdirectory of the PostgreSQL source tree and try it there without
+`pg_config`:
+
+ env NO_PGXS=1 make && make installcheck && make install
+
+If you encounter an error such as:
+
+ ERROR: must be owner of database regression
+
+You need to run the test suite using a super user, such as the default
+"postgres" super user:
+
+ make installcheck PGUSER=postgres
+
+Once geoip is installed, you can add it to a database. If you're running
+PostgreSQL 9.1.0 or greater, it's a simple as connecting to a database as a
+super user and running:
+
+ CREATE EXTENSION geoip;
+
+If you've upgraded your cluster to PostgreSQL 9.1 and already had geoip
+installed, you can upgrade it to a properly packaged extension with:
+
+ CREATE EXTENSION geoip FROM unpackaged;
+
+For versions of PostgreSQL less than 9.1.0, you'll need to run the
+installation script:
+
+ psql -d mydb -f /path/to/pgsql/share/contrib/geoip.sql
+
+If you want to install geoip and all of its supporting objects into a specific
+schema, use the `PGOPTIONS` environment variable to specify the schema, like
+so:
+
+ PGOPTIONS=--search_path=extensions psql -d mydb -f geoip.sql
25 LICENSE
@@ -0,0 +1,25 @@
+Copyright 2012, Tomas Vondra (tv@fuzzy.cz). All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are
+permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice, this list of
+ conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ of conditions and the following disclaimer in the documentation and/or other materials
+ provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY TOMAS VONDRA ''AS IS'' AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TOMAS VONDRA OR
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+The views and conclusions contained in the software and documentation are those of the
+authors and should not be interpreted as representing official policies, either expressed
+or implied, of Tomas Vondra.
@@ -0,0 +1,41 @@
+{
+ "name": "geoip",
+ "abstract": "Geolocation using GeoIP.",
+ "description": "Provides geolocation (determining country, city etc. from an IP address) using GeoIP data.",
+ "version": "0.0.1",
+ "maintainer": "Tomas Vondra <tv@fuzzy.cz>",
+ "license": "bsd",
+
+ "provides": {
+ "geoip": {
+ "file": "sql/geoip--0.0.1.sql",
+ "docfile": "doc/README.md",
+ "version": "0.0.1"
+ }
+ },
+
+ "prereqs": {
+ "runtime": {
+ "requires": {
+ "PostgreSQL": "8.3.0"
+ }
+ }
+ },
+
+ "release_status": "testing",
+
+ "resources": {
+ "repository": {
+ "url": "https://tvondra@github.com/tvondra/geoip.git",
+ "web": "http://github.com/tvondra/geoip",
+ "type": "git"
+ }
+ },
+
+ "tags" : ["geolocation", "geoip", "ip"],
+
+ "meta-spec": {
+ "version": "1.0.0",
+ "url": "http://pgxn.org/meta/spec.txt"
+ }
+}
@@ -0,0 +1,12 @@
+EXTENSION = geoip
+
+DATA = $(wildcard sql/*.sql)
+DOCS = $(wildcard doc/*.md)
+TESTS = $(wildcard test/sql/*.sql)
+REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
+REGRESS_OPTS = --inputdir=test --load-language=plpgsql
+PG_CONFIG = pg_config
+PG91 = $(shell $(PG_CONFIG) --version | grep -qE " 8\.| 9\.0" && echo no || echo yes)
+
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
159 README.md
@@ -0,0 +1,159 @@
+geoip
+=====
+
+This extension provides IP-based geolocation, i.e. you provide an IPv4
+address and the extension looks for info about country, city, GPS etc.
+
+To operate, the extension needs data mapping IP addresses to the other
+info, but these data are not part of the extension. A good free dataset
+is GeoLite from MaxMind (available at www.maxmind.com).
+
+Installation
+------------
+
+To install the extension, basically all you need to do is this
+
+ make install
+
+and then (if you're on PostgreSQL 9.1 or above)
+
+ CREATE EXTENSION geoip;
+
+For versions of PostgreSQL less than 9.1.0, you'll need to run the
+installation script manually:
+
+ psql -d mydb -f /path/to/pgsql/share/contrib/geoip.sql
+
+Now you're ready to use the extension. More details about the installation
+options and issues are available in the INSTALL file.
+
+
+Using the extension
+-------------------
+
+The extension allows you to search for country, city and ASN. All of that
+is encapsulated into these functions:
+
+ * geoip_country_code(inet) - returns country code (2 chars)
+ * geoip_country(inet) - returns all country info (code, name, ...)
+ * geoip_city_location(inet) - returns just location ID (INT)
+ * geoip_city(inet) - returns all the city info (GPS, ZIP code, ...)
+ * geoip_asn(inet) - retusn ASN name and IP range
+
+Using the functions is quite straightforward, especially for functions that
+return a single value
+
+ db=# SELECT geoip_country_code('78.45.133.255'::inet);
+
+ geoip_country_code
+ --------------------
+ CZ
+ (1 row)
+
+ db=# SELECT geoip_city_location('78.45.133.255'::inet);
+
+ geoip_city_location
+ ---------------------
+ 21235
+ (1 row)
+
+The functions that return a tuple are a bit more complicated. Probably the
+best way to call them is like a SRF:
+
+ db=# SELECT * FROM geoip_city('78.45.133.255'::inet);
+
+ loc_id | country | region | city | latitude | longitude | ...
+ --------+---------+--------+--------+----------+-----------+-----
+ 21235 | CZ | 52 | Prague | 50.0833 | 14.4667 | ...
+
+ db=# SELECT * FROM geoip_country('78.45.133.255'::inet);
+
+ begin_ip | end_ip | country | name
+ -----------+---------------+---------+----------------
+ 78.44.0.0 | 78.45.255.255 | CZ | Czech Republic
+ (1 row)
+
+ db=# SELECT * FROM geoip_asn('78.45.133.255'::inet);
+
+ begin_ip | end_ip | name
+ -----------+---------------+-----------------------------------
+ 78.44.0.0 | 78.45.255.255 | AS6830 UPC Broadband Holding B.V.
+ (1 row)
+
+Sure, you can access the data directly through the tables.
+
+Loading the data
+----------------
+This extension requires manual downloading and loading the data. Once
+you have the extension installed (so that the tables exist), go to
+http://www.maxmind.com and download the CSV files
+
+ * http://www.maxmind.com/app/geolitecountry - GeoIPCountryCSV.zip
+ * http://www.maxmind.com/app/geolitecity - GeoLiteCity_20120207.zip
+ * http://www.maxmind.com/app/asnum - GeoIPASNum2.zip
+
+Now we need to preprocess the CSV files so that it's possible to load
+them into the tables with a COPY. First, unzip the GeoIPCountryCSV.zip
+and remove the two columns with IP addresses encoded as INT values.
+
+ $ unzip GeoIPCountryCSV.zip
+ $ sed 's/^\("[^"]*","[^"]*",\)"[^"]*","[^"]*",\("[^"]*","[^"]*"\)/\1\2/' \
+ GeoIPCountryWhois.csv > countries.csv
+
+Now unzip the GeoLite city data and remove the first two rows (header)
+
+ $ tail -$((`wc -l GeoLiteCity-Blocks.csv | awk '{print $1}'`-2)) \
+ GeoLiteCity-Blocks.csv > blocks.csv
+
+ $ tail -$((`wc -l GeoLiteCity-Location.csv | awk '{print $1}'`-2)) \
+ GeoLiteCity-Location.csv > locations.csv
+
+It's time to load the data into the database. There's still a bit of
+transforming that needs to be done (and doing it in shell would be
+awkward), so we'll create a few temporary tables. So log in to the
+database and do this (the PATH needs to be replaced with an actual
+absolute path to the files).
+
+ COPY geoip_country FROM 'PATH/countries.csv'
+ WITH csv DELIMITER ',' NULL '' QUOTE '"' ENCODING 'ISO-8859-2';
+
+ CREATE TEMPORARY TABLE geoip_city_block_tmp (
+ begin_ip BIGINT NOT NULL,
+ end_ip BIGINT NOT NULL,
+ loc_id INTEGER NOT NULL
+ );
+
+ CREATE TEMPORARY TABLE geoip_asn_tmp (
+ begin_ip BIGINT NOT NULL,
+ end_ip BIGINT NOT NULL,
+ name TEXT NOT NULL
+ );
+
+ COPY geoip_city_block_tmp FROM 'PATH/blocks.csv'
+ WITH csv DELIMITER ',' NULL '' QUOTE '"' ENCODING 'ISO-8859-2';
+
+ COPY geoip_city_location FROM 'PATH/locations.csv'
+ WITH csv DELIMITER ',' NULL '' QUOTE '"' ENCODING 'ISO-8859-2';
+
+ COPY geoip_asn_tmp FROM 'PATH/GeoIPASNum2.csv'
+ WITH csv DELIMITER ',' NULL '' QUOTE '"' ENCODING 'ISO-8859-2';
+
+ INSERT INTO geoip_city_block
+ SELECT geoip_bigint_to_inet(begin_ip),
+ geoip_bigint_to_inet(end_ip), loc_id
+ FROM geoip_city_block_tmp;
+
+ INSERT INTO geoip_asn
+ SELECT geoip_bigint_to_inet(begin_ip),
+ geoip_bigint_to_inet(end_ip), name
+ FROM geoip_asn_tmp;
+
+ ANALYZE;
+
+Now the data is loaded.
+
+
+Copyright and License
+---------------------
+Copyright (c) 2012 Tomas Vondra
+The extension is distributed under BSD license (see the LICENSE file)
@@ -0,0 +1,33 @@
+geoip
+=====
+
+Synopsis
+--------
+
+ Show a brief synopsis of the extension.
+
+Description
+-----------
+
+A long description
+
+Usage
+-----
+
+ Show usage.
+
+Support
+-------
+
+ There is issues tracker? Github? Put this information here.
+
+Author
+------
+
+[The maintainer's name]
+
+Copyright and License
+---------------------
+
+Copyright (c) 2012 The maintainer's name.
+
@@ -0,0 +1,5 @@
+# geoip extension
+comment = 'An IP geolocation extension (a wrapper around the MaxMind GeoLite dataset)'
+default_version = '0.0.1'
+module_pathname = '$libdir/geoip'
+relocatable = true
Oops, something went wrong.

0 comments on commit 9668dd9

Please sign in to comment.