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

Posted

技术标签:

【中文标题】django.db.utils.OperationalError:致命:角色“django”不存在【英文标题】:django.db.utils.OperationalError: FATAL: role "django" does not exist 【发布时间】:2017-04-16 18:37:07 【问题描述】:

我跟随 this tutorial on Digital Ocean 在 Ubuntu 16.04 服务器上安装 PostgreSQL 9.5 以与 Django 1.10 一起使用。

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

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

我得到的错误:

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

我的 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',

hba_file

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

它的内容(好吧,反正相关部分):

# 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

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

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

我这辈子都搞不清楚哪里出了问题。

【问题讨论】:

这很奇怪,因为它应该给消息password authentication failed for user "django",它永远不会向未经身份验证的人透露用户是否存在。这一定是我们看不到的非常明显的东西。你能显示完整的回溯吗? 另外,在您的settings.py 中,尝试将用户“django”更改为“django1”和“postgres”(密码错误);在这些情况下它会给出什么错误消息? 当我将 USER 设置为 django1 时,我得到一个 django.db.utils.OperationalError: FATAL: role "django1" does not exist,当我将 postgres 设置为用户(密码错误)时,我得到一个 django.db.utils.OperationalError: FATAL: database "agencies" does not exist 我已在此处粘贴完整的跟踪 (user=django):pastebin.com/P3YL6dp5 经过调查,我们发现在一个被遗忘的 Docker 容器中还有第二个 PostgreSQL 实例。不确定这对其他用户是否有帮助,所以也许应该删除这个问题。 【参考方案1】:

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

GRANT ALL PRIVILEGES ON DATABASE agencies TO django;

【讨论】:

我确实添加了这些权限(根据我提到的教程),但确实它似乎没有显示在 \du 概述中。应该吗? PostgreSQL 不会说谎。当它说“不存在”时,就意味着“不存在”。 但是为什么我使用\du时会出现django这个角色? 嗯。我 100% 确定我授予了特权,但只是为了确保我重做了。现在访问权限具有数据库agencies 的条目。我会更新我的问题(因为我得到的错误仍然是一样的)。【参考方案2】:

参考链接click here。已经解释过了。

你的设置有

'用户':'django',

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

只需进入 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

然后在settings.py中

'密码':'密码',

密码不应包含在中。

【讨论】:

谢谢,但原因是在 Docker 容器中运行的第二个 postgresql 数据库。见***.com/questions/40922239/…【参考方案3】:

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

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

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

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

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

【讨论】:

你是对的 :) 见my comment on another answer to this question。 啊,酷。我没有看到。如果您愿意,请标记已回答/正确的问题! :)【参考方案4】:

如果您正在使用 Docker,并且它正在侦听 5432 端口,您应该杀死其他也在侦听的进程。

为此,请键入以下命令以查看哪些进程正在使用端口 5432:

$ lsof -i:5432

这将如下所示:

COMMAND     PID           USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
com.docke 15178 andre.carvalho   86u  IPv6 0xb128ac87cd34cc69      0t0  TCP *:postgresql (LISTEN)
postgres  16670 andre.carvalho    7u  IPv6 0xb128ac87dc50c569      0t0  TCP localhost:postgresql (LISTEN)
postgres  16670 andre.carvalho    8u  IPv4 0xb128ac87d2378541      0t0  TCP localhost:postgresql (LISTEN)

在此之后,很简单:只需杀死另一个进程,使用命令:

kill -9 16670

(注意进程是由PID标识的。)

【讨论】:

以上是关于django.db.utils.OperationalError:致命:角色“django”不存在的主要内容,如果未能解决你的问题,请参考以下文章