Django设置:psycopg2.OperationalError:致命:用户"indivo"的对等身份验证失败

mas*_*iny 40 python django postgresql psycopg2 django-settings

我在使用POSTGRESQL的Django项目设置中遇到问题.

这是我的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.
        },
}
Run Code Online (Sandbox Code Playgroud)

现在在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 
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我尝试syncdb时,我收到了错误.

psycopg2.OperationalError: FATAL:  Peer authentication failed for user "indivo"
Run Code Online (Sandbox Code Playgroud)

请帮我解决一下我在这里做错了什么.

jul*_*sar 46

我有类似的问题,并通过添加到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.
        },
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的回答,它也对我有用; 尽管评论说`#设置为localhost的空字符串.:( (6认同)

小智 26

默认情况下,在许多Linux发行版中,客户端身份验证设置为"对等",用于与数据库的Unix套接字连接.这是在pg_hba.confpostgresql 的配置文件中设置的.psycopg2 python库文档说明:

- *host*:数据库主机地址(如果未提供,则默认为UNIX套接字)

因此,如果将主机选项留空,则脚本将尝试连接并使用Unix用户名:密码进行身份验证.要修复,您可以:

  1. 将"host"选项显式设置为127.0.0.1到您粘贴到问题中的配置代码中.
  2. 修改pg_hba.conf文件以使用md5进行套接字连接,以便使用存储在postrgres DB用户存储中的用户名和密码(不推荐使用,因为这可能会破坏系统用户的身份验证)

  • 也许添加信息,你必须在编辑``pg_hba.conf``后重新启动postgresql服务器:``sudo service postgresl restart`` (2认同)

Cra*_*ger 6

您需要设置为用户,数据库和感兴趣的源IP pg_hba.conf使用md5身份验证.请参阅文档的客户端身份验证章节.

搜索pg_hba.confStack Overflow以获取更多信息.

  • @masterofdestiny老实说,你真的应该阅读手册.它涵盖了所有这些深度.在Ubuntu上,`pg_hba.conf`位于`/ etc/postgresql/9.1/main/pg_hba.conf`中.否则......这是所有手册,阅读[pg_hba.conf`上的文档](http://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html). (2认同)