-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
22 lines (18 loc) · 827 Bytes
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import os
hostname = os.environ.get('DB_HOSTNAME', 'localhost')
db_name = os.environ.get('DB_NAME', 'postgres')
username = os.environ.get('DB_USERNAME', 'postgres')
password = os.environ.get('DB_PASSWORD', 'mypassword')
port = os.environ.get('DB_PORT', '5432')
engine = create_engine(f'postgresql+psycopg2://{username}:{password}@{hostname}:{port}/{db_name}')
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
import models
Base.metadata.create_all(bind=engine)