04 May 2009

import data from csv into postgresql

We can use sql command, COPY. But programmer still need to create the table before copy the data. Simple python script to help


#!/usr/bin/python
fn = '/home/helmi/world.csv'
columns = file(fn).readline()
print 'create table world (%s text);' % " text,".join(columns.split(','))
print "copy world from '%s' with csv header;" % fn


then

python csv2psql.py | psql mydb
Post a Comment