fastapi-postgres-todo-api / psql_database.py
PrabhuKiranKonda's picture
initial commit
2f88c81
raw
history blame contribute delete
922 Bytes
import os
import urllib.parse as up
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
# Parse the DATABASE_URL using urllib.parse
up.uses_netloc.append("postgres")
url = up.urlparse(
"postgres://xphzyodo:jWMawSzATJaJGSkOP90KSucl2Ni9DEPG@john.db.elephantsql.com/xphzyodo")
# Create the connection string
conn_string = f'postgresql+psycopg2://{url.username}:{url.password}@{url.hostname}/{url.path[1:]}'
# Create the engine using the connection string
engine = sa.create_engine(conn_string)
# Reassign the engine to your existing engine variable
engine = engine
# Create the session factory
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Reassign the session factory to your existing SessionLocal variable
SessionLocal = SessionLocal
# Reassign the Base to your existing Base variable
Base = declarative_base()