Updated
This commit is contained in:
31
uv_app/core/pgsql.py
Normal file
31
uv_app/core/pgsql.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
import psycopg2
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Force reload environment variables from .env, ignoring system vars.
|
||||
load_dotenv(override=True)
|
||||
|
||||
DB_HOST = os.getenv("DB_HOST")
|
||||
DB_PORT = os.getenv("DB_PORT")
|
||||
DB_NAME = os.getenv("DB_NAME")
|
||||
DB_USER = os.getenv("DB_USER")
|
||||
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
||||
|
||||
|
||||
def connect_to_pgsql() -> Optional[psycopg2.extensions.connection]:
|
||||
"""Establish a connection to the PostgreSQL database."""
|
||||
try:
|
||||
return psycopg2.connect(
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
database=DB_NAME,
|
||||
user=DB_USER,
|
||||
password=DB_PASSWORD,
|
||||
# Set the client encoding to UTF-8.
|
||||
options="-c client_encoding=utf8",
|
||||
)
|
||||
except psycopg2.Error as exc:
|
||||
print(f"Error connecting to PostgreSQL database: {exc}")
|
||||
return None
|
||||
Reference in New Issue
Block a user