Created
April 24, 2017 14:36
-
-
Save kunanit/eb0723eef653788395bb41c661c1fa86 to your computer and use it in GitHub Desktop.
Read postgres database table into pandas 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 | |
# follows django database settings format, replace with your own settings | |
DATABASES = { | |
'production':{ | |
'NAME': 'dbname', | |
'USER': 'user', | |
'PASSWORD': 'pass', | |
'HOST': 'rdsname.clqksfdibzsj.us-east-1.rds.amazonaws.com', | |
'PORT': 5432, | |
}, | |
} | |
# choose the database to use | |
db = DATABASES['production'] | |
# construct an engine connection string | |
engine_string = "postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}".format( | |
user = db['USER'], | |
password = db['PASSWORD'], | |
host = db['HOST'], | |
port = db['PORT'], | |
database = db['NAME'], | |
) | |
# create sqlalchemy engine | |
engine = create_engine(engine_string) | |
# read a table from database into pandas dataframe, replace "tablename" with your table name | |
df = pd.read_sql_table('tablename',engine) |
Thanks for that useful code ;)
This is great, thanks
This is incredible, thank you so much for this!
Thanks so much for this code, very helpful :)
Worked as a charm
Thanks a lot for the code
Thanks @kunanit for the code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this, it was super helpful!