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'
Use Isolation-level constants from
psycopg2.extensions
instead of arbitrary integers.Set it on the connection with
Connection.set_isolation_level()
Or set it on the session with
set_session()
Also, why are you constructing the DSN string like that if you already have the host, db, pass, etc. separately? Use keyword args when connecting:
Or store the entire DSN as a single environment var and use that directly: