psycopg2.errors - Exception classes mapping PostgreSQL errors - Psycopg 2.9.7 documentation
Psycopg 2.9.7 documentation
      
       
        
         psycopg2.errors
        
       
      
      - Exception classes mapping PostgreSQL errors
      
       
      
     
     New in version 2.8.
Changed in version 2.8.4: added errors introduced in PostgreSQL 12
Changed in version 2.8.6: added errors introduced in PostgreSQL 13
Changed in version 2.9.2: added errors introduced in PostgreSQL 14
Changed in version 2.9.4: added errors introduced in PostgreSQL 15
      This module exposes the classes psycopg raises upon receiving an error from
the database with a
      
       
        SQLSTATE
       
      
      value attached (available in the
      
       
        
         pgcode
        
       
      
      attribute). The content of the module is generated
from the PostgreSQL source code and includes classes for every error defined
by PostgreSQL in versions between 9.1 and 15.
     
      Every class in the module is named after what referred as "condition name"
      
       in
the documentation
      
      , converted to CamelCase: e.g. the error 22012,
      
       
        division_by_zero
       
      
      is exposed by this module as the class
      
       
        DivisionByZero
       
      
      .
     
      Every exception class is a subclass of one of the
      
       
        standard DB-API
exception
       
      
      and expose the
      
       
        
         Error
        
       
      
      interface.
Each class’ superclass is what used to be raised by psycopg in versions before
the introduction of this module, so everything should be compatible with
previously written code catching one the DB-API class: if your code used to
catch
      
       
        IntegrityError
       
      
      to detect a duplicate entry, it will keep on working
even if a more specialised subclass such as
      
       
        UniqueViolation
       
      
      is raised.
     
The new classes allow a more idiomatic way to check and process a specific error among the many the database may return. For instance, in order to check that a table is locked, the following code could have been used previously:
try:
    cur.execute("LOCK TABLE mytable IN ACCESS EXCLUSIVE MODE NOWAIT")
except psycopg2.OperationalError as e:
    if e.pgcode == psycopg2.errorcodes.LOCK_NOT_AVAILABLE:
        locked = True
    else:
        raise
      While this method is still available, the specialised class allows for a more idiomatic error handler:
try:
    cur.execute("LOCK TABLE mytable IN ACCESS EXCLUSIVE MODE NOWAIT")
except psycopg2.errors.LockNotAvailable:
    locked = True
      - psycopg2.errors. lookup ( code )
 - 
       
Lookup an error code and return its exception class.
Raise
KeyErrorif the code is not found.try: cur.execute("LOCK TABLE mytable IN ACCESS EXCLUSIVE MODE NOWAIT") except psycopg2.errors.lookup("55P03"): locked = True
 
SQLSTATE exception classes
The following table contains the list of all the SQLSTATE classes exposed by the module.
       Note that, for completeness, the module also exposes all the
       
        
         DB-API-defined exceptions
        
       
       and
       
        
         a few
psycopg-specific ones
        
       
       exposed by the
       
        
         extensions
        
       
       module, which are not listed here.
      
| 
           SQLSTATE  | 
         
           Exception  | 
         
           Base exception  | 
        
|---|---|---|
| 
           Class 02 : No Data (this is also a warning class per the SQL standard)  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 03 : SQL Statement Not Yet Complete  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 08 : Connection Exception  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 09 : Triggered Action Exception  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 0A : Feature Not Supported  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 0B : Invalid Transaction Initiation  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 0F : Locator Exception  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 0L : Invalid Grantor  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 0P : Invalid Role Specification  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 0Z : Diagnostics Exception  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 20 : Case Not Found  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 21 : Cardinality Violation  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 22 : Data Exception  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 23 : Integrity Constraint Violation  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 24 : Invalid Cursor State  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 25 : Invalid Transaction State  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 26 : Invalid SQL Statement Name  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 27 : Triggered Data Change Violation  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 28 : Invalid Authorization Specification  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 2B : Dependent Privilege Descriptors Still Exist  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 2D : Invalid Transaction Termination  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 2F : SQL Routine Exception  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 34 : Invalid Cursor Name  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 38 : External Routine Exception  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 39 : External Routine Invocation Exception  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 3B : Savepoint Exception  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 3D : Invalid Catalog Name  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 3F : Invalid Schema Name  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 40 : Transaction Rollback  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 42 : Syntax Error or Access Rule Violation  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 44 : WITH CHECK OPTION Violation  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 53 : Insufficient Resources  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 54 : Program Limit Exceeded  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 55 : Object Not In Prerequisite State  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 57 : Operator Intervention  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 58 : System Error (errors external to PostgreSQL itself)  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class 72 : Snapshot Failure  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class F0 : Configuration File Error  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class HV : Foreign Data Wrapper Error (SQL/MED)  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class P0 : PL/pgSQL Error  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           Class XX : Internal Error  | 
        ||
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             | 
        
| 
           
             | 
         
           
             | 
         
           
             |