Postgres COPY FROM command does not work through Python

I have a Python script that uses the psycopg2 library to connect to Postgres and copy tables from the Postgres database to text files (tab-delimited). It works great. Then I try to run a similar function, but this time I take the text files and paste them into an identical table in another Postgres database. My script looks like this:

def copyFromFile(tableName):
    try:
        cTo = None
        cTo = psycopg2.connect(database="postgres", user="postgres", password="postgres", host="localhost")
        tCursor = cTo.cursor()
        print 'here'
        io = open(tableName+'.txt', 'r')
        tCursor.copy_from(io, tableName)
        print 'done copying'
    except Exception as inst:
        print "I am unable to copy to " + tableName

#start 
copyFromFile('schema.tableName')

The text file looks like this:

95216   8802    269726  3   1350    1   2014-09-02 14:11:25.817178  I
95217   8802    269726  4   1351    1   2014-09-02 14:11:25.817178  I
95218   8802    269726  5   1352    1   2014-09-02 14:11:25.817178  I
95219   8802    269726  6   1353    1   2014-09-02 14:11:25.817178  I
95220   8802    269726  7   1354    1   2014-09-02 14:11:25.817178  I
95221   8802    269726  8   1355    1   2014-09-02 14:11:25.817178  I
95222   8802    269726  9   1356    1   2014-09-02 14:11:25.817178  I



When I run this script, I get no errors, but the database table does not receive data. For starters, this table is empty. What could be missing?

+4
source share
1 answer
print 'done copying'
cTo.commit()

may be? -g

+2

All Articles