【发布时间】: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 实例。不确定这对其他用户是否有帮助,所以也许应该删除这个问题。
标签: django postgresql python-3.x digital-ocean ubuntu-16.04