Spaces:
Runtime error
Runtime error
File size: 1,395 Bytes
bd8cd5c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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")
|