Python 如何在本地运行 Postgres

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13784340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:36:43  来源:igfitidea点击:

How To Run Postgres locally

pythonpostgresqlherokuflask

提问by Rohit Rayudu

I read the Postgres docs for Flask

我阅读了 Flask 的 Postgres 文档

and they said that to run Postgres you should have the following code

他们说要运行 Postgres 你应该有以下代码

app = Flask(__name__) 
app.config['SQLALCHEMY_DATABASE_URI'] = postgresql://localhost/[YOUR_DB_NAME]' 
db = SQLAlchemy(app)

How do I know my database name?

我如何知道我的数据库名称?

I wrote db as the name - but I got an error

我写了 db 作为名称 - 但出现错误

sqlalchemy.exc.OperationalError: (OperationalError) FATAL:  database "[db]" 
does not exist

Running Heroku with Flask if that helps

如果有帮助的话,用 Flask 运行 Heroku

采纳答案by Burhan Khalid

First step, is to get Flask + Postgresql running locally, and the first step to do thatis to install postgresql on your machine. Next, you should install the python drivers for postgresql.

第一步,是让瓶+ PostgreSQL的本地运行,而第一步要做的是就是你的机器上安装PostgreSQL。接下来,您应该为 postgresql安装python 驱动程序

For Windows, you can use the windows installer for postgresqland the windows installer for the python drivers.

对于 Windows,您可以使用适用于 postgresqlWindows 安装程序和适用于 Python 驱动程序Windows 安装程序

Once you have finished the above two steps, you need to create a database.

完成上述两个步骤后,您需要创建一个数据库

For Windows, you can use the bundled pgadminIII tool. Here is a videothat shows how to do just that.

对于 Windows,您可以使用捆绑的 pgadminIII 工具。这是一个视频,展示了如何做到这一点。

For example, here is how you create a database called the_database, and create a user called databaseuserwith a password P@ssw0rdusing the built-in tool psql:

例如,这里是如何创建一个名为 的数据库the_database,并使用内置工具创建一个使用databaseuser密码调用的用户:P@ssw0rdpsql

$ 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

Here is how you would configure your application with the above information:

以下是使用上述信息配置应用程序的方法:

db_conn = 'postgresql+psycopg2://databaseuser:P@ssw0rd@localhost/the_database' 
app = Flask(__name__) 
app.config['SQLALCHEMY_DATABASE_URI'] = db_conn
db = SQLAlchemy(app)

回答by Ignas But?nas

I do not have experience with Heroku, but from your messages I would say you don't have PostgreSQL running and you dont have a database at all. Looks like here they have some info https://postgres.heroku.com/:)

我没有使用 Heroku 的经验,但是根据您的消息,我会说您没有运行 PostgreSQL,而且您根本没有数据库。看起来他们在这里有一些信息https://postgres.heroku.com/:)

But in your topic you asking about localhost PostgreSQL... So if you want to run your setup locally, first thing to check is if you have PostgreSQL installed and running. If you do, try to connect to it with pgAdmin or from console and create database. If you can't connect - check config for ports, hosts PostgreSQL is listening.

但是在您的主题中,您询问了 localhost PostgreSQL...因此,如果您想在本地运行您的设置,首先要检查您是否安装并运行了 PostgreSQL。如果这样做,请尝试使用 pgAdmin 或从控制台连接到它并创建数据库。如果您无法连接 - 检查端口配置,主机 PostgreSQL 正在侦听。

Anyway those messages I see you posted in question in comments, gives me only the idea that you don't have DB created or even running (or you have wrong config).

无论如何,我看到你在评论中发布的那些消息,只给了我一个想法,即你没有创建甚至运行数据库(或者你有错误的配置)。