Last active
March 21, 2022 13:44
-
-
Save 00krishna/9026574 to your computer and use it in GitHub Desktop.
Connect from python pandas to a postgresql database and pull data.
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
import psycopg2 as pg | |
import pandas.io.sql as psql | |
# get connected to the database | |
connection = pg.connect("dbname=mydatabase user=postgres") | |
dataframe = psql.frame_query("SELECT * FROM <tablename>", connection) |
# how to get pandas data from postgree sql using python
import psycopg2 as pg
import pandas.io.sql as psql
import pandas as pd
connection = pg.connect("host='102.153.103.22' dbname=dbtest user=admin password='passwords'")
#dataframe = psql.DataFrame("SELECT * FROM category", connection)
df = pd.read_sql_query('select * from category',con=connection)
print(df)
out in good pandas format:
id version description name
0 3410 0 alcohol alcohol
1 3411 0 animals animals
2 3412 0 beauty beauty
3 3413 0 berries berries
#exelent worked with
#pandas | 0.23.1 | 0.23.1
#psycopg2 | 2.7.5 | 2.7.5
maybe this article can help
https://www.datacamp.com/community/tutorials/tutorial-postgresql-python
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Frank, helpful snippet