user3422637
user3422637

Reputation: 4229

Internal Error on trying copy data to postgres table from python code using copy_from function of psycopg2

I did this in my python code, based on the documentation here and it's mention here

# anything can be used as a file if it has .read() and .readline() methods
data = StringIO.StringIO()
data.write('\n'.join(['Tom\tJenkins\t37',
                  'Madonna\t\N\t45',
                  'Federico\tDi Gregorio\t\N']))
data.seek(0)

curs.copy_from(data, 'distributors')

I got this error: Traceback (most recent call last): File "", line 1, in cursor.copy_from(data, 'distributors') InternalError: current transaction is aborted, commands ignored until end of transaction block

Upvotes: 1

Views: 642

Answers (1)

user3422637
user3422637

Reputation: 4229

Found the answer here:

There was an attempted query run of an erroneous query using the same database connection. (For details: I had run a copy_from execution on this db connection with incorrect data. It had failed. And, then I modified my data to be in correct format)

When your previous query/command on the database using this connection had a problem/error, it needs to be rolled back using connection.rollback() call. Once, you execute that code, and re-run the fresh correct code, you would get data to be properly copied from Python code to Postgres database.

Upvotes: 3

Related Questions