gpt4 book ai didi

postgresql - fe_sendauth : error sending password authentication

转载 作者:行者123 更新时间:2023-11-29 12:34:02 26 4
gpt4 key购买 nike

我现在正在学习 Django 并且正在关注 Django book一步一步。

当我继续第 5 章将我的 Django 项目连接到我的数据库时,我无法连接它。我收到错误:

FATAL:  password authentication failed for user "postgres"

代码如下:

settings.py 配置:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'postgis', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': 'XXXXXXX', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '8904', # Set to empty string for default. Not used with sqlite3.
}
}

但是当我使用这些代码在 Python shell 中测试连接和设置时:

from django.db import connection
cursor=connection.cursor()

shell 回复错误:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "E:\Python27\lib\site-packages\django-1.4.1- py2.7.egg\django\db\backends\__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "E:\Python27\lib\site-packages\django-1.4.1-py2.7.egg\django\db\backends\
postgresql_psycopg2\base.py", line 177, in _cursor
self.connection = Database.connect(**conn_params)
File "E:\Python27\lib\site-packages\psycopg2\__init__.py", line 179, in connect
connection_factory=connection_factory, async=async)
OperationalError: FATAL: password authentication failed for user "postgres"

然后我重新安装了 PostgreSQL 和 pycopg2 软件包。我启动 pgAdminIII 来运行服务器并连接到数据库,然后我在 python shell 中运行相同的代码,但问题仍然存在。

需要帮助!

最佳答案

猜测是您使用了错误的密码,或者您的 pg_hba.conf 配置不正确。

您能否使用具有所需凭据的 psql 进行连接?尝试:

psql -U postgres -W -p 8904 postgis

如果不是,请检查 pg_hba.conf 以查看 postgres 用户正在使用 identmd5 安全性。如果你想使用密码连接,它需要是 md5。如果更改设置,请使用 pg_ctl reload 或重新启动 PostgreSQL 以使更改生效。

您可以通过使用psql 连接到您的数据库并运行SHOW hba_file; 来找到pg_hba.conf。例如:

sudo -u postgres psql -c 'SHOW hba_file;'

请注意,无论如何,您都不应该使用 PostgreSQL super 用户进行应用程序连接。创建一个新用户并让 Django 在新数据库上使用该用户。例如,要创建一个名为 django 的用户和一个名为 django_db 并由 django 用户拥有的数据库:

sudo -u postgres createuser -SDR -P django
sudo -u postgres createdb -O django_db django;

-SDR 使默认值显式化,即 django 用户不应是 super 用户且不应有 CREATEDB<​​ CREATEUSER 权限。

关于postgresql - fe_sendauth : error sending password authentication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12027938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com