Python Django 设置:psycopg2.OperationalError:致命:用户“indivo”的对等身份验证失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15805561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django setting : psycopg2.OperationalError: FATAL: Peer authentication failed for user "indivo"
提问by masterofdestiny
I am getting problem in Django project setting with POSTGRESQL.
我在使用 POSTGRESQL 的 Django 项目设置中遇到问题。
Here is my setting.py database setting
这是我的 setting.py 数据库设置
DATABASES = {
'default':{
'ENGINE':'django.db.backends.postgresql_psycopg2', # '.postgresql_psycopg2', '.mysql', or '.oracle'
'NAME':'indivo', # Required to be non-empty string
'USER':'indivo', # Required to be non-empty string
'PASSWORD':'ritvik',
'HOST':'', # Set to empty string for localhost.
'PORT':'', # Set to empty string for default.
},
}
Now in postgres backend what I have done is .
现在在 postgres 后端我所做的是 .
rohit@rohit-desktop:~$ sudo su - postgres
postgres@rohit-desktop:~$ createuser --superuser indivo # create a super user indivo
postgres@rohit-desktop:~$ psql # open psql terminal
psql (9.1.8)
Type "help" for help.
postgres=# \password indivo # set the password ritvik
Enter new password:
Enter it again:
postgres=# \q #logout
postgres@rohit-desktop:~$ createdb -U indivo -O indivo indivo #create db indivo
Unfortunately when i am trying to syncdb I am getting the error .
不幸的是,当我尝试同步数据库时出现错误。
psycopg2.OperationalError: FATAL: Peer authentication failed for user "indivo"
Please help me out what might I am doing wrong here .
请帮我看看我在这里做错了什么。
回答by Craig Ringer
You need to set pg_hba.confto use md5authentication for the user, db and source IP of interest. See the client authenticationchapter of the documentation.
您需要设置pg_hba.conf对md5感兴趣的用户、数据库和源 IP使用身份验证。请参阅文档的客户端身份验证章节。
Search for pg_hba.confon Stack Overflow for tons more information.
pg_hba.conf在 Stack Overflow 上搜索更多信息。
回答by queso
By default in many Linux distros, client authentication is set to "peer" for Unix socket connections to the DB. This is set in the pg_hba.confconfig file for postgresql. The psycopg2 python library documentation states:
默认情况下,在许多 Linux 发行版中,对于到数据库的 Unix 套接字连接,客户端身份验证设置为“对等”。这是在pg_hba.confpostgresql的配置文件中设置的。psycopg2 python 库文档说明:
- *host*: database host address (defaults to UNIX socket if not provided)
- *host*: 数据库主机地址(如果未提供,则默认为 UNIX 套接字)
So if you leave the host option blank, your script will try to connect and use the Unix username:password for authentication. To fix, you can either:
因此,如果您将主机选项留空,您的脚本将尝试连接并使用 Unix 用户名:密码进行身份验证。要修复,您可以:
- set the "host" option explicitly to 127.0.0.1 into the config code you pasted into your question.
- Modify the
pg_hba.conffile to use md5 for socket connections so it uses usernames and passwords stored in the postrgres DB user store (not recommended as this may break authentication for system users)
- 将“主机”选项明确设置为 127.0.0.1 到您粘贴到问题中的配置代码中。
- 修改
pg_hba.conf文件以使用 md5 进行套接字连接,以便它使用存储在 postrgres 数据库用户存储中的用户名和密码(不推荐,因为这可能会破坏系统用户的身份验证)
回答by juliocesar
I have similar problem and solved it with this answerby adding localhostto the database HOSTsettings in settings.py, so your database settings should look like this:
我有类似的问题,并通过添加到settings.py 中的数据库设置来解决此问题,因此您的数据库设置应如下所示:localhostHOST
DATABASES = {
'default':{
'ENGINE':'django.db.backends.postgresql_psycopg2', # '.postgresql_psycopg2', '.mysql', or '.oracle'
'NAME':'indivo', # Required to be non-empty string
'USER':'indivo', # Required to be non-empty string
'PASSWORD':'ritvik',
'HOST':'localhost', # <- Changed from empty string to localhost
'PORT':'', # Set to empty string for default.
},
}
回答by Gopal Prasad
I too was facing similar problem during setting up postgresql database with Django project.
在使用 Django 项目设置 postgresql 数据库时,我也遇到了类似的问题。
My System is running RHEL CentOS 7 with postgresql version 10. I search a lot but couldnot find actually what is happening until I saw log at /var/lib/pqsql/10/data/log/postgresql*.log
我的系统正在运行带有 postgresql 版本 10 的 RHEL CentOS 7。我搜索了很多但找不到实际发生的事情,直到我在 /var/lib/pqsql/10/data/log/postgresql*.log 看到日志
2017-11-10 00:31:40.499 IST [14129] DETAIL: Connection matched pg_hba.conf line 85: "host all all ::1/128 ident"
2017-11-10 00:31:40.499 IST [14129] 详细信息:连接匹配 pg_hba.conf 第 85 行:“host all all ::1/128 ident”
So I just changed that particular line in file /var/lib/pgsql/10/data/pg_hba.conf : from host all all ::1/128 ident
所以我只是更改了文件 /var/lib/pgsql/10/data/pg_hba.conf 中的特定行:来自主机所有 ::1/128 ident
to
到
host all all ::1/128 md5
主持所有所有:: 1/128 md5
with that I am able to create database & table using manage.py migrate
这样我就可以使用 manage.py migrate 创建数据库和表
Thanks for all who has helped me to find this solution. especially @juliocesar and official documentation for postgresql client authentication
感谢所有帮助我找到此解决方案的人。特别是@juliocesar 和 postgresql 客户端身份验证的官方文档

