-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_utils.py
48 lines (35 loc) · 1.28 KB
/
database_utils.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import psycopg2
import sqlalchemy
import yaml
from sqlalchemy import Engine, create_engine, engine_from_config, text, inspect
class DatabaseConnector:
def read_db_creds(self):
try:
with open('db_creds.yaml') as f:
creds = yaml.safe_load(f)
return creds
except:
print("Failed to read cred")
def init_db_engine(self):
creds = self.read_db_creds()
HOST = creds['RDS_HOST']
USER = creds['RDS_USER']
PASSWORD = creds['RDS_PASSWORD']
DATABASE = creds['RDS_DATABASE']
PORT = creds['RDS_PORT']
DATABASE_TYPE = 'postgresql'
DBAPI = 'psycopg2'
engine = create_engine(f"{DATABASE_TYPE}+{DBAPI}://{USER}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}")
return engine
def list_db_tables(self):
try:
engine = self.init_db_engine()
with engine.connect() as connection:
result = connection.execute(text("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'"""))
for row in result:
print(row)
except Exception as e:
print("Failed to fecth tables.", e)
""""d = DatabaseConnector()
d.list_db_tables()"""