fe_sendauth:发送密码验证时出错

Ian*_*hao 1 postgresql database-connection postgis django-settings python-2.7

我现在正在学习Django,并且正在逐步关注Django的书.

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

FATAL:  password authentication failed for user "postgres"
Run Code Online (Sandbox Code Playgroud)

这是代码:

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.
}
}
Run Code Online (Sandbox Code Playgroud)

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

from django.db import connection
cursor=connection.cursor()
Run Code Online (Sandbox Code Playgroud)

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

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

需要帮忙!

Cra*_*ger 5

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

您可以使用psql所需的凭据连接吗?尝试:

psql -U postgres -W -p 8904 postgis
Run Code Online (Sandbox Code Playgroud)

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

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

sudo -u postgres psql -c 'SHOW hba_file;'
Run Code Online (Sandbox Code Playgroud)

请注意,您永远不应该为应用程序连接使用PostgreSQL超级用户.创建一个新用户并让Django在新数据库上使用该用户.例如,要创建一个名为的用户django和一个名为django_dbdjango用户所拥有的数据库:

sudo -u postgres createuser -SDR -P django
sudo -u postgres createdb -O django_db django;
Run Code Online (Sandbox Code Playgroud)

-SDR使得默认的明确,即django用户不应该是一个超级用户,不应该有CREATEDBCREATEUSER权利.


bbu*_*rky 5

在 PostgreSQL 14+ 中,有一种获取fe_sendauth: error sending password authentication错误的新方法。

PostgreSQL 14 发行说明中所述,客户端和服务器现在都使用 OpenSSL EVP API,该 API 强制执行 FIPS 模式。如果您使用密码md5,并且您的 FIPS 配置不允许,您将无法使用 PostgreSQL 14 登录md5

password_encryption您可以使用检查默认设置SHOW password_encryption;,并使用以下命令手动更新单个用户:

SET password_encryption  = 'scram-sha-256'; 
ALTER USER "user" with password 'password';
Run Code Online (Sandbox Code Playgroud)

要永久更改password_encryption新用户密码的默认设置,请更新postgresql.conf

password_encryption = scram-sha-256
Run Code Online (Sandbox Code Playgroud)

来自 PostgreSQL 14 发行说明:

  • 更改 SHA1、SHA2 和 MD5 哈希计算以使用 OpenSSL EVP API (Michael Paquier)

    这是更现代的并且支持 FIPS 模式。

  • 将密码_加密服务器参数的默认值更改为scram-sha-256(Peter Eisentraut)

    以前是md5。除非更改此服务器设置或以 MD5 格式指定密码,否则所有新密码都将存储为 SHA256。此外,以前作为同义词的遗留(和未记录的)类布尔值md5不再被接受。