Python не видит переменную

Python не видит переменную connection, хотя я ее прописываю в блоке try. Подскажите, пожалуйста, в чем может быть дело.

import psycopg2
from file2 import host, user, password, db_name

try:
    connection = psycopg2.connect(host=host, user=user, password=password, database=db_name)

    with connection.cursor() as cursor:
        cursor.execute('SELECT version();')
        print(f'Server version: {cursor.fetchone()}')
except Exception as ex:
    print('[INFO] Error while working with PostgreSQL', ex)
finally:
    if connection:
        connection.close()
        print('[INFO] PostgreSQL connection closed')

Ответы (0 шт):