我阅读了 Flask 的 Postgres 文档
他们说要运行 Postgres 你应该有下面的代码
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = postgresql://localhost/[YOUR_DB_NAME]'
db = SQLAlchemy(app)
我怎么知道我的数据库名称?
我写了 db 作为名字 - 但我得到了一个错误
sqlalchemy.exc.OperationalError: (OperationalError) FATAL: database "[db]"
does not exist
如果有帮助,用 Flask 运行 Heroku
最佳答案
第一步,是让 Flask + Postgresql 在本地运行,那的第一步是 install postgresql on your machine .接下来,您应该安装 python drivers for postgresql .
对于 Windows,您可以使用 windows installer for postgresql和 windows installer for the python drivers .
完成上述两个步骤后,您需要create a database .
对于 Windows,您可以使用捆绑的 pgadminIII 工具。这是一个video这说明了如何做到这一点。
例如,下面是如何创建一个名为 the_database
的数据库,并使用密码 P@ssw0rd
创建一个名为 databaseuser
的用户内置工具psql
:
$ psql -d template1 -U postgres
template1=# CREATE USER databaseuser WITH PASSWORD 'P@ssw0rd';
template1=# CREATE DATABASE the_database;
template1=# GRANT ALL PRIVILEGES ON DATABASE the_database to databaseuser;
template1=# \q
以下是您将如何使用上述信息配置您的应用程序:
db_conn = 'postgresql+psycopg2://databaseuser:P@ssw0rd@localhost/the_database'
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = db_conn
db = SQLAlchemy(app)
关于python - 如何在本地运行 Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13784340/