AP257
AP257

Reputation: 93773

psycopg2.InternalError: how can I get more useful information?

I'm running this command in a Python script:

try: 
    print sql_string
    cursor.execute(sql_string)
except:
    print sys.exc_info()

and getting:

(<class 'psycopg2.InternalError'>, InternalError('current transaction is aborted, commands ignored until end of transaction block\n',), <traceback object at 0x1010054d0>)

However if I try the sql_string from the psql command line, it works just fine. I know the script is connecting to the database okay, because I can run other commands.

How can I get Python to give me more useful information about why this command is failing within the script?

Upvotes: 10

Views: 13672

Answers (2)

nathancahill
nathancahill

Reputation: 10850

You can also tail the output of postgresql to see the query that caused the error:

$ tail -f /var/log/postgresql/postgresql.log

This is often easier than editing the script to debug it.

Upvotes: 8

Matthew Wood
Matthew Wood

Reputation: 16417

Try this:

try:
    print sql_string
    cursor.execute(sql_string)
except Exception, e:
    print e.pgerror

If you are still getting "current transaction is aborted, commands ignored until end of transaction block" then your error is further back in your transaction and this query is only failing due to a previous query failing (and thereby invalidating the entire transaction).

The details for pgerror can be found in the documentation at http://initd.org/psycopg/docs/module.html#exceptions

Upvotes: 10

Related Questions