I'm trying to connect with psycopg: v3.1.12
to PostgreSQL
and set the Isolation Level to 1.
I've tried adding the set_isolation_level(1) found in the official Docs in both the connection and the transaction set up but it doesn't work for me.
Any help would be appreciated.
My connection :
self.pg_connection = psycopg.connect(
(
"dbname="
+ os.environ["PG_DB"]
+ " user="
+ os.environ["PG_USER"]
+ " password="
+ os.environ["PG_PASS"]
+ " host="
+ os.environ["PG_HOST"]
+ " port="
+ os.environ["PG_PORT"]
),
row_factory=dict_row,
autocommit=False,
)
My Transaction :
with self.pg_connection.transaction() as tx:
tx.set_isolation_level(1)
The above returns the exception :
Exception: 'Transaction' object has no attribute 'set_isolation_level'
According to the docs, Connection.set_isolation_level()
was only added in v3.2
So on v3.1.12 you need to use
Connection.isolation_level
. Moreover you need to set this on the connection object, not the transaction object itself.
import psycopg
from psycopg import IsolationLevel
conn = psycopg.connect(...)
conn.isolation_level = IsolationLevel.READ_UNCOMMITTED
# If you're on psycopg >=v3.2 you can also use
# conn.set_isolation_level(IsolationLevel.READ_UNCOMMITTED)
with conn.transaction() as tx:
...