python - 我的本地 postgresql 数据库 url 的形式是什么?

标签 python postgresql flask sqlalchemy

我正在阅读 flask/sqlalchemy 教程 https://pythonhosted.org/Flask-SQLAlchemy/quickstart.html#a-minimal-application 我将我的数据库 url 配置为: postgres://用户名:密码@localhost:5432/数据库名

当我在交互式 python shell 中运行 db.create_all() 时,它不会抛出任何错误,但它也不会执行任何操作。

据我了解,应该创建包含三列的用户表; ID、用户名和电子邮件。

from flask import Flask, url_for, render_template 
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://username:password@localhost:5432/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.debug = True



@app.route("/")
def hello():
    return render_template('hello.html')

if __name__ == "__main__":
    app.run()

最佳答案

应该是准确的格式。

app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:postgres@localhost/DBNAME"

其中postgres为用户名,postgres为密码,localhost为地址

关于python - 我的本地 postgresql 数据库 url 的形式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24727902/

相关文章:

postgresql - 为什么不能在表上并行执行 truncate 和 grant 语句?

python - 引入 flask_restful 后,flask errorhandler 在 gunicorn 中不起作用

python - 如何在 Flask 的扩展名为 .jhtml 的模板中启用自动转义?

Python ZeroMQ模块导入报错

python - 映射递归关系时出现问题 - SQLAlchemy

python - 为什么 numpy.prod() 对于我的一长串自然数错误地返回负结果或 0?

postgresql - 为什么 pgAdmin III 不能连接到 Postgres?

Python帮助命令

postgresql - now() 默认值都显示相同的时间戳

python - flask url_for 类型错误