Mike Girard
Mike Girard

Reputation: 464

No errors logged with failed Postgres/Psycopg2 copy_from

The following code does not elicit an error. But it doesn't put any values in my database either. Have taken all constraints off the table except index on Primary Key. The two fields are both strings. Any ideas? The most confusing thing is that no errors get logged.

conn = psycopg2.connect("dbname=<mydbname> user=postgres password=<mypassword>")
cur = conn.cursor()
output = StringIO.StringIO()
output.write('Citizen Caine\tMy_API_id\n')
cur.copy_from(output, 'movie', columns=('title','api_id'))
conn.commit()

Upvotes: 19

Views: 3062

Answers (1)

notbad.jpeg
notbad.jpeg

Reputation: 3368

I struggled with this one too. Some hidden knowledge is that you have to do an:

output.seek(0) #put the position of the buffer at the beginning

after your write, or if you're moving from database to database, after you do a copy_to.

It's easy to forget that StringIO objects have all the same methods and attributes of a file object.

Upvotes: 27

Related Questions