加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – Flask&Alchemy – (psycopg2.OperationalEr

发布时间:2020-12-13 18:05:59 所属栏目:百科 来源:网络整理
导读:我是 python的新手.我必须使用PostgreSQL作为数据库开发一个简单的Flask应用程序(在我的本地Ubuntu 16.4中). 我安装了pgadmin,Flask,SQLAlchemy和postgres,这也是我的应用代码: from flask import Flaskfrom flask_sqlalchemy import SQLAlchemyapp = Flask
我是 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

06005

但它不适合我.

我遇到了同样的错误.
对我来说问题是我没有为psql用户设置密码.
在这里看到类似的问题和答案:
https://askubuntu.com/questions/413585/postgres-password-authentication-fails

我这样做就解决了

ALTER USER db_username PASSWORD 'new_password'

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读