postgresql django.db.utils.OperationalError:致命:角色“django”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40922239/
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-10-21 02:27:41  来源:igfitidea点击:

django.db.utils.OperationalError: FATAL: role "django" does not exist

djangopostgresqlpython-3.xdigital-oceanubuntu-16.04

提问by LaundroMat

I followed this tutorial on Digital Oceanto install PostgreSQL 9.5 on an Ubuntu 16.04 server to use with Django 1.10.

我按照Digital Ocean 上的本教程在Ubuntu 16.04 服务器上安装 PostgreSQL 9.5 以与 Django 1.10 一起使用。

Everything went smoothly, but I can't get my Django app to connect to the database (or so it seems). App and database are on the same server.

一切都很顺利,但我无法让我的 Django 应用程序连接到数据库(或者看起来如此)。应用程序和数据库在同一台服务器上。

Here are some settings, configs and reports:

以下是一些设置、配置和报告:

The error I get:

我得到的错误:

File "/home/mathieu/web/agencies/lib/python3.5/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL:  role "django" does not exist

My Django project's database settings:

我的 Django 项目的数据库设置:

DATABASES = {
'sqlite3': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
},
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'agencies',
    'USER': 'django',
    'PASSWORD': '<password>',
    'HOST': 'localhost',
    'PORT': '5432',
}}

The hba_file

hba_file

postgres=# SHOW hba_file;
hba_file
--------------------------------------
/etc/postgresql/9.5/main/pg_hba.conf

Its contents(well, the relevant part anyway):

它的内容(好吧,无论如何都是相关部分):

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

Users and database in psql

psql中的用户和数据库

postgres=# \du
                               List of roles
Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
django    |                                                            | {}
postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}


postgres=# \l
                              List of databases
Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
agencies  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres         +
                                                            |  postgres=CTc/postgres+
                                                            |  django=CTc/postgres
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
          |          |          |             |             |  postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
          |          |          |             |             | postgres=CTc/postgres

I followed the exact same steps on a VM (running Linux Mint, I should say) and all went fine and dandy...

我在虚拟机(运行 Linux Mint,我应该说)上遵循完全相同的步骤,一切都很好,花花公子......

I can't for the life of me figure out what's or where things are going wrong.

我一生都无法弄清楚出了什么问题或哪里出了问题。

回答by Rob

You may see this error if you have postgres installed both locally (and running) and a docker container both trying to occupy the same port.

如果您在本地(并正在运行)和 docker 容器都安装了 postgres 并试图占用同一个端口,您可能会看到此错误。

If the local instance starts first and is occupying a port that the docker image is also trying to use, docker won't necessarily tell you this.

如果本地实例首先启动并且占用了 docker 镜像也尝试使用的端口,则 docker 不一定会告诉您这一点。

When you try to run django or other ./manage.pycommands that need the database you'll see the same error because the app will not see the database you're looking for.

当您尝试运行 django 或其他./manage.py需要数据库的命令时,您会看到相同的错误,因为应用程序不会看到您正在寻找的数据库。

In this case, you can change the port on your locally installed postgres by stopping the server, clicking Server Settingsand changing the port. You'll have to update settings.pyon any apps you have depending on that old port.

在这种情况下,您可以通过停止服务器、单击Server Settings并更改端口来更改本地安装的 postgres 上的端口。您settings.py必须根据旧端口更新您拥有的任何应用程序。

In my experience, after doing this if you restart the docker db container you won't see this error anymore.

根据我的经验,执行此操作后,如果您重新启动 docker db 容器,您将不会再看到此错误。

回答by Astik Anand

Refer the link click here. It is explained.

请参阅链接单击此处。它被解释了。

Your settings has

你的设置有

'USER': 'django',

'USER': 'django',

But as error says that user doesn't exist , that means you have not created the user.

但是由于错误提示用户不存在,这意味着您还没有创建用户。

Just go into interactive session of psql and enter these commands.

只需进入 psql 的交互式会话并输入这些命令。

CREATE DATABASE agencies;
CREATE USER django WITH PASSWORD 'password';
ALTER ROLE django SET client_encoding TO 'utf8'; 
ALTER ROLE django SET default_transaction_isolation TO 'read committed'; 
ALTER ROLE django SET timezone TO 'Asia/Kolkata';

GRANT ALL PRIVILEGES ON DATABASE agencies TO django;
\q

then in settings.py

然后在settings.py

'PASSWORD': 'password',

'密码':'密码',

password should not be enclosed inside < >.

密码不应包含在 < > 内。

回答by okay

I think you forgot to add priveleges to user 'django':

我认为您忘记为用户“django”添加特权:

GRANT ALL PRIVILEGES ON DATABASE agencies TO django;