我是 python 的新手。我必须使用 PostgreSQL 作为数据库开发一个简单的 Flask 应用程序(在我本地的 Ubuntu 16.4 中)。
我安装了 pgadmin、Flask、SQLAlchemy 和 postgres,这也是我的应用程序代码:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://dbUserName:userNamePassword@localhost/dbName'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username, email):
self.username = username
self.email = email
def __repr__(self):
return '<User %r>' % self.username
@app.route('/')
def index():
return "Hello Flask"
if __name__ == "__main__":
app.run()
我还在 pgAdmin 中创建了一个数据库和新用户(并在我的代码中用相关变量替换它们),但是当我尝试在 python shell 中测试这段代码时,我发现了错误。
我的 python 代码:
from app import db
结果:
/home/user/point2map2/venv/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py:839: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
然后:
db.create_all()
结果:
(psycopg2.OperationalError) FATAL: password authentication failed for user "dbUserName"
FATAL: password authentication failed for user "dbUserName"
在论坛中进行大量搜索后,我找到了这个指南:
in your pg_hba.conf
# IPv4 local connections: # TYPE DATABASE USER CIDR-ADDRESS METHOD host all all 127.0.0.1/32 trust
但这对我不起作用。
最佳答案
我遇到了同样的错误。 我的问题是我没有为 psql 用户设置密码。 在此处查看带有答案的类似问题: https://askubuntu.com/questions/413585/postgres-password-authentication-fails
当我这样做的时候它就解决了
ALTER USER db_username PASSWORD 'new_password'
关于postgresql - Flask & Alchemy - (psycopg2.OperationalError) fatal error : password authentication failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43657126/