Created
May 6, 2013 15:12
-
-
Save geobabbler/5525759 to your computer and use it in GitHub Desktop.
Script to load IP blocks from MaxMind GeoLiteCity into a PostgreSQL database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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