Ok that makes sense and clarifies some stuff for me. I have tried your implementation but it doesn't seem like its getting new connections. We are using sessionmaker(). So basically this is what we are doing. Can you help me understand if we are doing this right and if any changes need to happen to make this work? Sorry the tabbing is not right after paste. Thanks for your help!
# Create the engine to connect to the database
engine = create_engine(
f"postgresql+psycopg2://test:password@{pg_host}:5432/{pg_database}",
# connect_args=ssl_args,
connect_args={"options": "-c timezone=utc"},
pool_pre_ping=True,
encoding="utf8",
)
@event.listens_for(engine, "do_connect")
def receive_do_connect(dialect, conn_rec, cargs, cparams):
# Getting the postgres details
try:
# Get the configs
configs = Properties()
# Open the file to get the values needed
with open("/var/secrets/pgsql/vault-credentials.properties", "rb") as config_file:
configs.load(config_file)
# Get each of the properties, hopefully
pg_user = configs.get("username").data
pg_password = configs.get("password").data
except FileNotFoundError:
# Use whats in the environment
pg_user = os.getenv("pg_user")
pg_password = os.getenv("pg_password")
print("Connecting to db with username: ", pg_user)
print("Connecting to db with password: ", pg_password)
cparams["user"] = pg_user
cparams["password"] = pg_password
session_factory = sessionmaker(bind=engine)
sqla_session = session_factory()
# Then using the sqla_session to execute queries and make modifications to the database