What is the form of my local postgresql database url?

二次信任 提交于 2020-08-27 03:30:40

问题


I am going through a flask/sqlalchemy tutorial https://pythonhosted.org/Flask-SQLAlchemy/quickstart.html#a-minimal-application and I configured my database url to be: postgres://username:password@localhost:5432/dbname

when I run db.create_all() in the interactive python shell it doesn't throw any errors, but it doesn't do anything either.

From what I understand it is supposed to create the User table with three columns; id, username, and email .

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()

回答1:


It should be the exact format.

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

Where postgres is username and postgres is password, localhost is address




回答2:


Try using postgresql in the URL instead of postgres.

That's the form that's used both for JDBC and in the SQLAlchemy doc (http://docs.sqlalchemy.org/en/rel_0_9/dialects/postgresql.html).



来源:https://stackoverflow.com/questions/24727902/what-is-the-form-of-my-local-postgresql-database-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!