In [ ]:
from logging import info, error
import traceback
import subprocess
import sys
from sqlalchemy import create_engine, text
import pandas as pd
In [ ]:
DIALECT = "postgresql"
USER = "x"
PWD = "x"
HOST = "x"
DB = "x" # Specify the DB name
SCHEMA = "x"
DB_TABLE = "the_table_name"
In [ ]:
def setup_db(dialect, user, password, host, db, create=False):
"""
Setup database connection
"""
info_str = "{}@{}/{}".format(user, host, db)
connection_str = "{}://{}:{}@{}/{}".format(dialect, user, password, host, db)
if create:
subprocess.call(
["export PGPASSWORD={};"
"createdb -h {} -U {} {}".format(password, host, user, db)], shell=True)
info("DB created: {}.".format(info_str))
engine = create_engine(connection_str)
info("The DB is connected: {}.".format(info_str))
return engine
engine = setup_db(DIALECT, USER, PWD, HOST, DB)
In [ ]:
df = pd.read_sql_table(TABLE, engine, schema=SCHEMA)