django.db.utils.OperationalError:致命:角色“django"不存在 [英] django.db.utils.OperationalError: FATAL: role "django" does not exist
问题描述
我关注了 本教程关于 Digital Ocean 在 Ubuntu 16.04 服务器上安装 PostgreSQL 9.5 以与 Django 1.10 一起使用.
I followed this tutorial on Digital Ocean to install PostgreSQL 9.5 on an Ubuntu 16.04 server to use with Django 1.10.
一切都很顺利,但我无法让我的 Django 应用程序连接到数据库(或者看起来如此).应用程序和数据库在同一台服务器上.
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.
以下是一些设置、配置和报告:
Here are some settings, configs and reports:
我得到的错误:
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
它的内容(好吧,还是相关的部分):
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
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)并且一切顺利……
I followed the exact same steps on a VM (running Linux Mint, I should say) and all went fine and dandy...
我终其一生都无法弄清楚出了什么问题或哪里出了问题.
I can't for the life of me figure out what's or where things are going wrong.
推荐答案
如果您在本地(并正在运行)安装了 postgres 并且尝试占用同一个端口的 docker 容器,您可能会看到此错误.
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 镜像也尝试使用的端口,那么 docker 不一定会告诉您这一点.
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.
当您尝试运行需要数据库的 django 或其他 ./manage.py
命令时,您会看到相同的错误,因为应用程序不会看到您正在寻找的数据库.
When you try to run django or other ./manage.py
commands that need the database you'll see the same error because the app will not see the database you're looking for.
在这种情况下,您可以通过停止服务器,单击 Server Settings
并更改端口来更改本地安装的 postgres 上的端口.您必须根据旧端口在您拥有的任何应用程序上更新 settings.py
.
In this case, you can change the port on your locally installed postgres by stopping the server, clicking Server Settings
and changing the port. You'll have to update settings.py
on any apps you have depending on that old port.
根据我的经验,执行此操作后,如果您重新启动 docker db 容器,您将不会再看到此错误.
In my experience, after doing this if you restart the docker db container you won't see this error anymore.
这篇关于django.db.utils.OperationalError:致命:角色“django"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!