Last active
June 14, 2023 22:50
-
-
Save wwarriner/c18c3851a6fe8fb01086e6305b992089 to your computer and use it in GitHub Desktop.
How to: use Python with sqlalchemy into postgresql
This file contains 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
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 |
This file contains 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
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"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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