-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB_utility_funcs.py
32 lines (27 loc) · 936 Bytes
/
DB_utility_funcs.py
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
#This file contains all the database helper functions
from typing import Any
import psycopg2
import sys
import configparser
def connect_to_database() -> Any :
'''
Function to establish the connection to the database
Arguments : None
Returns : the connection object to the database
'''
cred_file = "./config.cfg"
db_config = configparser.ConfigParser()
db_config.read(cred_file)
try:
conn = psycopg2.connect(
host=db_config['DATABASE']['DB_HOST'],
port=db_config['DATABASE']['DB_PORT'],
database=db_config['DATABASE']['DB_NAME'],
user=db_config['DATABASE']['DB_USER'],
password=db_config['DATABASE']['DB_PASSWORD'],
)
conn.set_session(autocommit=True)
return conn
except Exception as e:
print("Encountered an error in connecting to the database : ", e)
sys.exit()