django.db.utils.OperationalError: FATAL: role "django"不存在



我在数字海洋上遵循本教程,在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

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

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

如果您在本地安装了Postgres(和运行)和Docker容器,则可能会看到此错误。

如果本地实例首先开始并占据了Docker映像也试图使用的端口,则Docker不一定告诉您。

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

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

根据我的经验,如果您重新启动Docker DB容器后,您将不再看到此错误。

请参考链接单击此处。解释了。

您的设置有

'用户':'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

然后在设置中。py

'密码':'密码',

密码不应在&lt内包含密码;>。

如果您正在使用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'添加私有:

GRANT ALL PRIVILEGES ON DATABASE agencies TO django;

相关内容

最新更新