我是python和postgres的新手,我正在尝试构建一个简单的烧瓶网络应用程序 . 用户可以选择一个csv文件,按上传,它将在我的服务器上使用csv中的表在postgres中创建一个表 . 一个简单的复制粘贴 .
my html code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>upload</title>
</head>
<body>
<form method="POST" enctype=multipart/form-data action ="\upload">
<input type="file" name="user_file">
<input type="submit">
</form>
</body>
</html>
my python module:
from flask import Flask, render_template, send_file, request
import pandas as pd
import csv
import sqlalchemy as sa
app = Flask(__name__)
@app.route("/upload", methods = ['GET', 'POST'])
def upload():
if request.method == 'POST' and 'user_file' in request.files:
csvfile = request.files['user_file']
con=sa.create_engine('postgresql://user:password@localhost/dbname')
chunks = pd.read_csv(csvfile, chunksize=100000)
for chunk in chunks:
chunk.to_sql(name='table', if_exist='append', con=con)
return render_template('new.html')
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)
当我尝试创建一个到postgres服务器的连接字符串,创建一个表,并将csv复制到它,我得到一个网页: sqlalchemy.exc.OperationalError OperationalError: (psycopg2.OperationalError) could not connect to server: Connection timed out Is the server running on host "_localhost" and accepting TCP/IP connections on port 5432? (Background on this error at: http://sqlalche.me/e/e3q8)
尝试在线找到答案没有运气 . 非常感谢您的帮助,谢谢!