import psycopg2.extras import os import psycopg2 from psycopg2 import pool from dotenv import load_dotenv load_dotenv() class GetDB: def __init__(self): self.host = os.getenv("host") self.database = os.getenv("database") self.user = os.getenv("user") self.password = os.getenv("password") self.port = os.getenv("port") def get_db_connection(self): try: postgreSQL_pool = psycopg2.pool.SimpleConnectionPool(1, 20, host=self.host, database=self.database, user=self.user, password=self.password, port=self.port) # Use getconn() to Get Connection from connection pool return postgreSQL_pool except (Exception, psycopg2.DatabaseError) as error: print("Error while connecting to PostgreSQL", error) # # finally: # # closing database connection. # # use closeall() method to close all the active connection if you want to turn of the application # if postgreSQL_pool: # postgreSQL_pool.closeall # print("PostgreSQL connection pool is closed")