2013-04-04 36 views
40

我在使用POSTGRESQL的Django項目設置中遇到問題。Django設置:psycopg2.OperationalError:致命:用戶「indivo」的同級身份驗證失敗

這裏是我的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. 
     }, 
} 

現在Postgres後端我做了什麼是。

[email protected]:~$ sudo su - postgres 
[email protected]:~$ createuser --superuser indivo # create a super user indivo 
[email protected]:~$ 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 
[email protected]:~$ createdb -U indivo -O indivo indivo #create db indivo 

不幸的是,當我試圖syncdb我收到錯誤。

psycopg2.OperationalError: FATAL: Peer authentication failed for user "indivo" 

請幫我看看我在這裏做錯了什麼。

+0

重複回答過的問題在這裏:Django的連接到PostgreSQL:「對等身份驗證失敗」] [1] [1]:http://stackoverflow.com/questions/ 8167602/Django的連接到PostgreSQL的對等實體認證失敗 – user3404455 2014-07-22 23:59:25

回答

6

您需要設置pg_hba.conf以使用md5身份驗證用戶,db和感興趣的源IP。請參閱文檔的client authentication章節。

在Stack Overflow上搜索pg_hba.conf以獲取更多信息。

+0

請幫助我如何在PostgreSQL的9.0 – masterofdestiny 2013-04-04 08:06:23

+2

@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)。 – 2013-04-04 08:08:29

+0

這一行對我來說是這樣:host all all 127.0.0.1/32 md5 – Harlin 2016-04-30 13:45:23

26

默認情況下,在很多Linux發行版中,對於與數據庫的Unix套接字連接,客戶端身份驗證設置爲「peer」。這在postgresql的pg_hba.conf配置文件中設置。該psycopg2 Python庫文檔指出:

- *host*: database host address (defaults to UNIX socket if not provided)

因此,如果您將主機選項空白,你的腳本將嘗試連接並使用Unix的用戶名:認證密碼。要解決,您可以:

  1. 將「主機」選項明確爲127.0.0.1到您粘貼到你的問題的配置代碼。
  2. 修改pg_hba.conf文件,以便它使用存儲在postrgres DB用戶存儲的用戶名和密碼,使用套接字連接的md5(不推薦,因爲這可能對系統用戶突破認證)
+2

也許增加信息,你必須在編輯「pg_hba.conf」後重新啓動postgresql服務器:sudo service postgresl restart '' – 2015-03-10 10:47:54

43

我有類似的問題和解決它this answer加入localhost到數據庫中settings.py HOST設置,讓你的數據庫的設置應該是這樣的:

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', # Set to empty string for localhost. 
     'PORT':'', # Set to empty string for default. 
     }, 
} 
+5

謝謝你的回答,它也適用於我;儘管事實上,評論說'#設置爲本地主機的空字符串。:( – contradictioned 2014-01-09 18:53:06

+0

是的,這也爲我工作,謝謝噸。 – Amyth 2014-05-21 11:26:41

0

我也正面臨着類似的問題和Django設置PostgreSQL數據庫中項目。

我的系統運行RHEL CentOS 7和postgresql版本10.我搜索了很多,但實際上找不到發生了什麼,直到我看到/var/lib/pqsql/10/data/log/postgresql*.log日誌爲止

2017年11月10日00:31:40.499 IST [14129]詳細信息:連接匹配的pg_hba.conf行85: 「主機上的所有一切:: 1/128的ident」

所以,我只是改變了特定的行文件/ var/lib/pgsql/10/data/pg_hba。CONF: 從 主機的所有一切:: 1/128的ident

主機的所有一切:: 1/128 MD5

與我能夠創建一個使用manage.py數據庫&表遷移

感謝所有誰幫我找到這個解決方案。 PostgreSQL的客戶端驗證尤其是@juliocesar和官方文件