python导出postgresql中的一个表到本地csv表格
代码如下 修改xxx即可:
conn = psycopg2.connect(host=DB_SERVICES, user=DB_USERNAME,password=DB_PWD, database=DB_NAME) cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) sql = f"select * from xxx.xxx" cur.execute(sql) res = cur.fetchall() pandas.DataFrame([dict(item) for item in res]).to_csv("xxx.csv",index=False,encoding="utf-8")