Postgresql用户没有连接到数据库(Nginx Django Gunicorn)

差不多一个月了,我一直在努力解决这个问题.每当我尝试在生产时访问我的Django Admin页面时,我都会收到以下错误:

OperationalError at /admin/login/
FATAL:  password authentication failed for user "vpusr"
FATAL:  password authentication failed for user "vpusr"

我的production.py设置文件如下:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql','NAME': 'vpdb','USER': 'vpusr','PASSWORD': os.environ["VP_DB_PASS"],'HOST': 'localhost',}
}

注意:环境变量正常工作.即使我把普通密码硬编码在那里它也行不通.

以下是其所有者的数据库列表:

                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 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
 vpdb      | vpusr    | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/vpusr            +
           |          |          |             |             | vpusr=CTc/vpusr

以下是用户列表:

                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser,Create role,Create DB,Replication,Bypass RLS | {}
 vpusr     | Superuser,Create DB                                       | {}

正如您所看到的,我还尝试将超级用户和创建数据库的角色添加到vpusr,但这没有任何效果.

即使我尝试通过这样的终端连接,我也得到同样的错误:

sudo -u postgres psql -U vpusr vpdb

我仍然得到错误:psql:致命:用户“vpusr”的对等身份验证失败

当我执行此命令时:

psql -U vpusr -h localhost vpdb

我正确连接到psql作为vpusr.

还有一些注意事项:我确实删除了数据库,然后用户重新创建了它们.我确保密码是正确的.
我在Digital Ocean的Ubuntu服务器上使用了Gunicorn,Nginx,Virtualenv,Django,Postgres.

提前感谢您花时间阅读并帮助我!

编辑:我注意到我的应用程序迁移文件夹中没有迁移!可能是django或我的用户或postgres没有写文件的权限?

编辑:注意:我改变了用户托尼
在我的postgres日志文件中,发现以下错误:

    2017-09-09 18:09:55 UTC [29909-2] LOG:  received fast shutdown request
2017-09-09 18:09:55 UTC [29909-3] LOG:  aborting any active transactions
2017-09-09 18:09:55 UTC [29914-2] LOG:  autovacuum launcher shutting down
2017-09-09 18:09:55 UTC [29911-1] LOG:  shutting down
2017-09-09 18:09:55 UTC [29911-2] LOG:  database system is shut down
2017-09-09 18:09:56 UTC [2711-1] LOG:  database system was shut down at 2017-09-09 18:09:55 UTC
2017-09-09 18:09:56 UTC [2711-2] LOG:  MultiXact member wraparound protections are now enabled
2017-09-09 18:09:56 UTC [2710-1] LOG:  database system is ready to accept connections
2017-09-09 18:09:56 UTC [2715-1] LOG:  autovacuum launcher started
2017-09-09 18:09:57 UTC [2717-1] [unknown]@[unknown] LOG:  incomplete startup packet
2017-09-09 18:10:17 UTC [2740-1] tony@vpdb LOG:  provided user name (tony) and authenticated user name (postgres) do not match
2017-09-09 18:10:17 UTC [2740-2] tony@vpdb FATAL:  Peer authentication failed for user "tony"
2017-09-09 18:10:17 UTC [2740-3] tony@vpdb DETAIL:  Connection matched pg_hba.conf line 90: "local   all             all                                     peer"

编辑:

pg_hba.conf文件:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# 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            password
# 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

你能说出什么呢?

最佳答案
您的应用程序正在尝试使用密码身份验证方法连接到PostgreSQL,但在您的pg_hba.conf文件中,连接类型与md5方法匹配,因此它期望进行md5身份验证.我们可以在您的日志消息中看到这一点

2017-09-01 11:42:17 UTC [16320-1] vpusr@vpdb FATAL:  password authentication failed for user "vpusr"
2017-09-01 11:42:17 UTC [16320-2] vpusr@vpdb DETAIL:  Connection matched pg_hba.conf line 92: "host    all             all             127.0.0.1/32            md5"

在PostgreSQL数据目录中找到pg_hba.conf文件,修改pg_hba.conf文件并更新该行

host    all             all             127.0.0.1/32            md5

并将其更改为

host    all             all             127.0.0.1/32            password

然后重新启动PostgreSQL服务

[root@server] service postgresql restart

然后尝试再次进行身份验证

要在运行命令时展开您看到的其他消息,请执行以下操作:
sudo -u postgres psql -U vpusr vpdb
你没有传递-h< host>参数,因此连接将尝试匹配该行

local    all             all             127.0.0.1/32            

因此,您需要检查本地连接所需的身份验证方法,并以此方式进行身份验证,或者传递-h< host>参数,然后它将匹配您的行

host    all             all             127.0.0.1/32            password

这意味着您可以在提示时输入密码,或者将连接字符串更改为

sudo -u postgres -c "PGPASSWORD=

编辑:如果这不能解决您的问题,您可以提供pg_hba.conf文件中的所有行

相关文章

文章浏览阅读3.7k次,点赞2次,收藏5次。Nginx学习笔记一、N...
文章浏览阅读1.7w次,点赞14次,收藏61次。我们在使用容器的...
文章浏览阅读1.4k次。当用户在访问网站的过程中遇到404错误时...
文章浏览阅读2.7k次。docker 和 docker-compose 部署 nginx+...
文章浏览阅读1.3k次。5:再次启动nginx,可以正常启动,可以...
文章浏览阅读3.1w次,点赞105次,收藏182次。高性能:Nginx ...