Getting error messages from psycopg2 exceptions
When I try to catch exceptions, e.pgerror is always None for connection errors. The following code block gets around this by directly printing 'e'.
try:
conn = psycopg2.connect(conn_string)
except psycopg2.OperationalError as e:
print('Unable to connect!\n{0}').format(e)
sys.exit(1)
else:
print('Connected!')
# do stuff
For example, in the case of password authentication failure:
Unable to connect!
FATAL: password authentication failed for user "user"
I realize this question is a year old but hopefully might help someone in the future
Ended up here because of
class 'psycopg2.errors.InvalidCursorName'
on Django. If that's your case, be sure to makemigrations
You are catching all exceptions with the base class psycopg2.Error
. Your problem is probably that the diag
attribute is new in psycopg2 2.5
. What is your version?
>>> print psycopg2.__version__
2.5.1 (dt dec pq3 ext)