Last active
May 9, 2018 08:23
-
-
Save georgerichardson/d1beb48d6aef10a38bcb019f328e8ae3 to your computer and use it in GitHub Desktop.
PostgreSQL to DataFrame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from sqlalchemy import create_engine | |
from sqlalchemy.engine.url import URL | |
# db settings to make url (could be in external file) | |
db_settings = {'drivername': <database driver>, # 'postgres', 'mysql' etc. | |
'host': <host address>, # 'localhost' etc. | |
'username': <username>, | |
'password': <password>, | |
'database': <database name> | |
} | |
# create connection url | |
url = URL(**db_settings) | |
# or provide url directly | |
# url = <database driver>://<user>:<password>@<host>/<databse name>' | |
def sql_table_to_df(table_name, url): | |
engine = create_engine(url) | |
conn = engine.connect() | |
with engine.connect() as conn, conn.begin(): | |
df = pd.read_sql_table(table_name, con=conn) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment