-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
28 lines (22 loc) · 897 Bytes
/
database.py
File metadata and controls
28 lines (22 loc) · 897 Bytes
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
import os
from dotenv import load_dotenv
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Base
load_dotenv()
class DatabaseConnector:
def __init__(self):
user = os.getenv("DB_USER")
password = os.getenv("DB_PASS")
host = os.getenv("DB_HOST")
db_name = os.getenv("DB_NAME")
self.db_url = f"postgresql://{user}:{password}@{host}:5432/{db_name}"
self.engine = create_engine(self.db_url, echo=True) # echo truth value determines whether database interactions are output to terminal or not
Session = sessionmaker(bind=self.engine)
self.session = Session()
def connect(self):
Base.metadata.create_all(self.engine)
print("Database tables verified and created successfully.")
if __name__ == "__main__":
db = DatabaseConnector()
db.connect()