4

I have a Python script that uses the psycopg2 library to connect to Postgres and copy tables from the a Postgres database to text files (tab-separated). This works perfectly. I'm then trying to run a similar function, but this time taking the text files and inserting them into an identical table in a different Postgres database. My script is as follows:

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 gets no data. This table is empty to start with. What could be missing?

1 Answer 1

3
print 'done copying'
cTo.commit()

maybe? -g

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.