Created
February 18, 2020 14:56
-
-
Save evu/e93533e0a9d21d6d49596d7fe7996346 to your computer and use it in GitHub Desktop.
Read from postgreSQL table into pandas dataframe using psycopg2 / sql query
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 | |
import pandas.io.sql as pdsql | |
import psycopg2 | |
# Database connection details | |
creds = { | |
"dbname": "mydb", | |
"user": "jsmith", | |
"password": "hello1234", | |
"host": "database.hostname.company.com", | |
"port": 5432, | |
"sslmode": "require", | |
} | |
with psycopg2.connect(**creds) as conn: | |
query = "select * from mytable limit 10;" | |
df = pdsql.read_sql_query(query, conn) | |
# If you have date columns to parse: | |
# df = pdsql.read_sql_query(query, conn, parse_dates=["created_ts"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment