Не удается подключится к postgreSQL
У меня такой код подключения к posrtgreSQL в Python Выдает ошибку: NameError: name 'connection' is not defined Подскажите, где я не прав?
try:
with psycopg2.connect(host=host,
user=user,
password=password,
database=db_name,
port=port) as connection:
with connection.cursor() as cursor:
cursor.execute("SELECT version();")
print(f"Server version: {cursor.fetchone()} ")
connection.close()
print('[INFO] PostgreSQL connection closed')
except Exception as _ex:
print('[INFO] Error while working with postgreSQL', _ex)
[INFO] Error while working with postgreSQL
[INFO] Error while working with postgreSQL
Источник: Stack Overflow на русском