Skip to content

Instantly share code, notes, and snippets.

@wwarriner
Last active June 14, 2023 22:50
Show Gist options
  • Save wwarriner/c18c3851a6fe8fb01086e6305b992089 to your computer and use it in GitHub Desktop.
Save wwarriner/c18c3851a6fe8fb01086e6305b992089 to your computer and use it in GitHub Desktop.
How to: use Python with sqlalchemy into postgresql
name: sqlalchemy
channels:
- conda-forge
dependencies:
- pandas=2.0.2
- pip=22.3.1
- python=3.10.9
- sqlalchemy=1.4.39
- pip:
- psychopg2-binary==2.9.6
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
import pandas as pd
p = {
"drivername": "postgresql+psycopg2",
"username": "",
"password": "",
"database": "",
"host": "",
"port": "",
}
url = URL.create(**p)
engine = create_engine(url)
conn = engine.connect()
df = pd.read_sql_query("SELECT COUNT(*) FROM jobs.info", conn)
print(df.loc[0, "count"])
@mdefende
Copy link

That is much more concise. The reason I didn't use SQLalchemy before was because I saw it struggles much more with large dataframes that the code I have in the postgres.py file. But for exploratory stuff on our end, that will work well

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