简体   繁体   中英

Postgres COPY FROM command not working through Python

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?

print 'done copying'
cTo.commit()

maybe? -g

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM