Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marymlucas/8ac1b1466639edbb09eb5ad48b4c5fae to your computer and use it in GitHub Desktop.
Save marymlucas/8ac1b1466639edbb09eb5ad48b4c5fae to your computer and use it in GitHub Desktop.
Read SQL query from psycopg2 into pandas dataframe
import pandas as pd
import pandas.io.sql as sqlio
import psycopg2
conn = psycopg2.connect("host='{}' port={} dbname='{}' user={} password={}".format(host, port, dbname, username, pwd))
sql = "select count(*) from table;"
dat = sqlio.read_sql_query(sql, conn)
conn = None
@marymlucas
Copy link
Author

marymlucas commented Feb 2, 2022

import pandas as pd

conn = psycopg2.connect("host='{}' port={} dbname='{}' user={} password={}".format(host, port, dbname, username, pwd))
sql = "select count(*) from table;"
dat = pd.read_sql_query(sql, conn)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment