I have this db_utils.py section where I define newconnect():
def newconnect():
with psycopg.connect("host=aaa port=5432 dbname=ccc user=bbb password=xxx") as conn:
print("Connected")
cur = conn.execute('SELECT version()')
db_version = cur.fetchone()
print(db_version)
print("Returning Cursor", cur)
return(cur)
This approach works with psycopg2, but with psyopg3, when I call this method:
cur = newconnect()
It prints out the Postgres version number as expected, but the returned cursor seems to be operating on a closed connection. When I do
cur.execute(some_sql) # main program that called db_utils/newconnect()
results = cur.fetchall()
print(results)
It does not execute that some_sql string. Instead,
psycopg.OperationalError: the connection is closed
How do I adjust the above to avoid this error?