"""Database engine/session wiring. DATABASE_URL comes from env (set by docker-compose). On the CT host it falls back to the local postgres port mapping using POSTGRES_PASSWORD from .env. """ import os from dotenv import load_dotenv from sqlalchemy import create_engine from sqlalchemy.orm import declarative_base, sessionmaker # /opt/polaris/.env exists on the CT host; harmless no-op elsewhere. load_dotenv(os.getenv("POLARIS_ENV_FILE", "/opt/polaris/.env"), override=False) DATABASE_URL = os.getenv( "DATABASE_URL", f"postgresql+psycopg2://reseller:{os.getenv('POSTGRES_PASSWORD', '')}@127.0.0.1:5432/reseller", ) engine = create_engine(DATABASE_URL, pool_pre_ping=True, pool_size=5, max_overflow=10) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() def get_db(): db = SessionLocal() try: yield db finally: db.close()