38 lines
1,002 B
Python
38 lines
1,002 B
Python
![]() |
"""
|
||
|
Database utility functions for the OpenEventDatabase.
|
||
|
"""
|
||
|
|
||
|
import os
|
||
|
import sys
|
||
|
import psycopg2
|
||
|
import psycopg2.extras
|
||
|
from oedb.utils.logging import logger
|
||
|
|
||
|
def db_connect():
|
||
|
"""
|
||
|
Connect to the PostgreSQL database using environment variables.
|
||
|
|
||
|
Returns:
|
||
|
psycopg2.connection: A connection to the database.
|
||
|
"""
|
||
|
return psycopg2.connect(
|
||
|
dbname=os.getenv("DB_NAME", "oedb"),
|
||
|
host=os.getenv("DB_HOST", ""),
|
||
|
password=os.getenv("POSTGRES_PASSWORD", None),
|
||
|
user=os.getenv("DB_USER", ""))
|
||
|
|
||
|
def check_db_connection():
|
||
|
"""
|
||
|
Check if the database is accessible.
|
||
|
|
||
|
Returns:
|
||
|
bool: True if the database is accessible, False otherwise.
|
||
|
"""
|
||
|
try:
|
||
|
conn = db_connect()
|
||
|
conn.close()
|
||
|
logger.success("Successfully connected to PostgreSQL database")
|
||
|
return True
|
||
|
except psycopg2.OperationalError as e:
|
||
|
logger.error(f"Failed to connect to PostgreSQL database: {e}")
|
||
|
return False
|