I am using psycopg2 with python3.10.4 to connect to a database, like this:
import os
import psycopg2
import psycopg2.extras
from psycopg2._psycopg import connection as pgconnection
class DbConnection:
con : pgconnection = None
def connect(self):
dbname = os.getenv('POSTGRES_DB')
user = os.getenv('POSTGRES_USER')
self.con = psycopg2.connect(
dbname=dbname,
user=user,
cursor_factory=psycopg2.extras.RealDictCursor)
def __execute(self, fetch: bool, query, vars=...) -> list[tuple[Any]]:
if not self.con or self.con.closed != 0:
self.connect()
with self.con as con:
with con.cursor() as cur:
cur.execute(query, vars)
if fetch:
return cur.fetchall()
return None
I use this to execute a set of migration scripts, one of them being
ALTER SYSTEM SET max_connections TO '100';
When I do this, I get ERROR: ALTER SYSTEM cannot run inside a transaction block
From my understanding, psycopg2 executes in transactions and fails to perform the ALTER SYSTEM statement because it is a non-transactional method. So the question is:
How do I execute a non-transactional statement like ALTER SYSTEM using psycopg2?