Skip to content

Instantly share code, notes, and snippets.

@geobabbler
Created May 6, 2013 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geobabbler/5525759 to your computer and use it in GitHub Desktop.
Save geobabbler/5525759 to your computer and use it in GitHub Desktop.
Script to load IP blocks from MaxMind GeoLiteCity into a PostgreSQL database.
import site
import psycopg2
import csv
conn = psycopg2.connect("dbname=mydb host=myhost user=myuser password=mypass")
cur = conn.cursor()
cr = csv.reader(open("/path/to/my.csv","rb"))
i = 0
for row in cr:
print i
cur.execute("""INSERT INTO ipblocks (startipnum, endipnum, locid) VALUES (%s,%s,%s)""", (row[0],row[1],row[2]));
i = i + 1
#commit every 50 records
if i%50 == 0:
conn.commit()
#uncomment following lines to performance test on first 300 rows
#if i == 300:
# break
cur.close()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment