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

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


python csv2psql.py | psql mydb


Anonymous said...

Helped me out, This code works great thanks.

Eddie Matos

.: Mas Sumarno :. said...

how to do it using php ?