Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 277
AP257 Avatar asked Sep 15 '10 13:09

AP257


People also ask

Why is psycopg2 needed?

It is designed to perform heavily multi-threaded applications that usually create and destroy lots of cursors and make a large number of simultaneous INSERTS or UPDATES. Psycopg features client-side and server-side cursors, asynchronous communication, and notification. Psycopg 2 is both Unicode and Python 3 friendly.

Is psycopg2 connection thread-safe?

Thread and process safetyThe Psycopg module and the connection objects are thread-safe: many threads can access the same database either using separate sessions and creating a connection per thread or using the same connection and creating separate cursors.

Is psycopg2 asynchronous?

Asynchronous notificationsPsycopg allows asynchronous interaction with other database sessions using the facilities offered by PostgreSQL commands LISTEN and NOTIFY.

What is cursor in psycopg2?

The Cursor class of the psycopg library provide methods to execute the PostgreSQL commands in the database using python code. Using the methods of it you can execute SQL statements, fetch data from the result sets, call procedures. You can create Cursor object using the cursor() method of the Connection object/class.


1 Answers

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

like image 99
Matthew Wood Avatar answered Nov 15 '22 18:11

Matthew Wood