-
Notifications
You must be signed in to change notification settings - Fork 12
Python PostgreSQL
Shuang edited this page Nov 2, 2017
·
1 revision
import psycopg2
sql = """
SELECT *
FROM test
"""
try:
conn = psycopg2.connect(database='xx', user='root', host='localhost', port='5432', password='password')
cur = conn.cursor()
cur.execute(sql)
ver = cur.fetchall()
except:
print "I am unable to connect to the database"
Return sql values as Pandas Dataframe
from sqlalchemy import create_engine
import pandas as pd
engine = create_engine('postgresql://root:password@localhost:5432/xx')
df = pd.read_sql_query(sql, con=engine)
[[TOC]]