Skip to content

Instantly share code, notes, and snippets.

@evu
Created February 18, 2020 14:56
Show Gist options
  • Save evu/e93533e0a9d21d6d49596d7fe7996346 to your computer and use it in GitHub Desktop.
Save evu/e93533e0a9d21d6d49596d7fe7996346 to your computer and use it in GitHub Desktop.
Read from postgreSQL table into pandas dataframe using psycopg2 / sql query
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