方法一:
import psycopg2
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine(“postgresql+psycopg2://**root**:**password**@**localhost**:**3306/trust**”,encoding=‘utf-8’)
sql = “”“select * from testexcel”""
df = pd.read_sql_query(sql,con=engine)
方法二:
import psycopg2
import pandas as pd
postgres_host = ""
postgres_port = "5432"
postgres_user = ""
postgres_password = ""
postgres_datebase = ""
postgres_table1 = ""
conn_string = "host=" + postgres_host + " port=" + postgres_port + " dbname=" + postgres_datebase +
" user=" + postgres_user + " password=" + postgres_password
conn = psycopg2.connect(conn_string)
sql_command1 = "select * from test"
try:
data1 = pd.read_sql(sql_command1, conn)
except:
print("load data from postgres failure !")
exit()