Switch to psycopg rather than psycopg2

It's a bit more modern and uses dedicated c bindings
This commit is contained in:
Dirk Müller 2024-05-16 11:18:01 +02:00
parent 1e22c2895a
commit 28d5c6e606
No known key found for this signature in database
2 changed files with 9 additions and 11 deletions

View File

@ -1,4 +1,4 @@
sudo zypper in python3-psycopg2 sudo zypper in python3-psycopg
sudo su - postgres sudo su - postgres
# `createdb -O <LOCAL_USER> imported_git` # `createdb -O <LOCAL_USER> imported_git`

View File

@ -1,7 +1,6 @@
import logging import logging
import psycopg2 import psycopg
from psycopg2.extras import LoggingConnection
from lib.config import config from lib.config import config
@ -17,11 +16,10 @@ class DB:
# read the connection parameters # read the connection parameters
params = config(section=self.config_section) params = config(section=self.config_section)
# connect to the PostgreSQL server # connect to the PostgreSQL server
self.conn = psycopg2.connect(connection_factory=LoggingConnection, **params) self.conn = psycopg.connect(conninfo=f"dbname={params['database']}")
logger = logging.getLogger(__name__) logging.getLogger("psycopg.pool").setLevel(logging.INFO)
self.conn.initialize(logger)
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg.DatabaseError) as error:
print(error) print(error)
raise error raise error
@ -32,7 +30,7 @@ class DB:
# execute a statement # execute a statement
try: try:
cur.execute("SELECT MAX(version) from scheme") cur.execute("SELECT MAX(version) from scheme")
except psycopg2.errors.UndefinedTable as error: except psycopg.errors.UndefinedTable as error:
cur.close() cur.close()
self.close() self.close()
self.connect() self.connect()
@ -273,7 +271,7 @@ class DB:
cur.execute(command) cur.execute(command)
# commit the changes # commit the changes
self.conn.commit() self.conn.commit()
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg.DatabaseError) as error:
print(error) print(error)
self.close() self.close()
raise error raise error