Django-postgresql: psycopg2 数据源名称使用了错误的密码

0 投票
1 回答
621 浏览
提问于 2025-04-18 13:56

我正在使用 python 2.7.3django 1.5.1,并且数据库是 postgresql。在我设置了一个简单的django应用后,出现了一个错误:OperationalError at FATAL: password authentication failed for user "postgres"。我检查了一下本地变量,发现psycopg2连接使用了错误的密码。虽然我在django的settings.py文件中指定了正确的密码。

另外,manage.py syncdb可以正常工作,但这个问题大约每三次中就会出现两次。

所有的认证方法在 pg_hba.conf 文件中都设置为 md5,而在settings.py中 HOST 设置为 localhost

1 个回答

0

不要用postgres这个用户来做这些事情。

为你的应用创建一个新的用户和数据库,只用这个用户来操作。

su
su postgres
createuser -P <dbusername>
:type and re-type <dbuserpasswd>
createdb -O <dbusername> <dbname>

检查一下你的 pg_hba.conf 文件。

local   all    all    md5

重启你的postgresql服务器。

检查一下你的Django设置。

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'dbname',                      # Or path to database file if using sqlite3.
    # The following settings are not used with sqlite3:
    'USER': 'dbusername',
    'PASSWORD': 'dbuserpasswd',
    'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
    'PORT': '',                      # Set to empty string for default.
    }
}

重启你的http服务器。

撰写回答