Skip to content

Instantly share code, notes, and snippets.

@jakebrinkmann
Created July 3, 2017 14:19
Show Gist options
  • Select an option

  • Save jakebrinkmann/de7fd185efe9a1f459946cf72def057e to your computer and use it in GitHub Desktop.

Select an option

Save jakebrinkmann/de7fd185efe9a1f459946cf72def057e 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
@SwapnilShirke

Copy link
Copy Markdown

nice , this quick solution and helpful

@naolip

naolip commented May 28, 2021

Copy link
Copy Markdown

Thx

@Exganza

Exganza commented Jul 19, 2021

Copy link
Copy Markdown

Very helpful
THANK YOU

@xanderladd

Copy link
Copy Markdown

thanks 👍

@jackycwwang

Copy link
Copy Markdown

Thanks for sharing the knowledge!

@Witomi

Witomi commented Dec 14, 2021

Copy link
Copy Markdown

Thanks a lot!

@evanrrees

Copy link
Copy Markdown

Perfect, thank you!

@marymlucas

Copy link
Copy Markdown

Most excellent! Thank you.

@saarthaksangam

Copy link
Copy Markdown

❤️

@efaraneda

Copy link
Copy Markdown

beautiful :D

ghost commented Aug 24, 2022

Copy link
Copy Markdown

this is awesome
Thanks alot.

@moenk

moenk commented Dec 5, 2022

Copy link
Copy Markdown

The example does not work for me:
UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql_query(sql, conn)

@aymenkrifa

aymenkrifa commented Mar 2, 2023

Copy link
Copy Markdown

> The example does not work for me: `UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy. df = pd.read_sql_query(sql, conn)

It should actually work and extract your table though because it's just a UserWarning, a warning that you can ignore (but it's not recommended to ignore it)
In Pandas, Psycopg2 is not tested as the warning implies, and it prefers using SQLAlchemy instead.

You can remake the example above by running the following code:

import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.sql import text

engine = create_engine(
    "{dialect}+{driver}://{username}:{password}@{host}:{port}/{database}".format(
        dialect="postgresql",
        driver="psycopg2",
        username="john",
        password="12345",
        host="localhost",
        port=5432,
        database="items"
    )
)

with engine.connect() as db_conn:
    sql_query = "SELECT COUNT(*) FROM table_name"
    df = pd.read_sql_query(sql=text(sql_query), con=db_conn)

You can actually create an engine by passing a URL object instead of the string:

from sqlalchemy import create_engine, URL

url_object = URL.create(
    drivername="DIALECT+DRIVER",
    username="username",
    password="password",
    host="host",
    database="database_name",
)

engine = create_engine(url=url_object)

I'm not an expert but I hope it helps!

@dss010101

Copy link
Copy Markdown

does anyone now if pd.read_sql blocks? im have a multi-thread process that uses it, and it seems to block

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