【发布时间】:2021-11-19 15:44:59
【问题描述】:
我的环境
操作系统:AWS linux
蟒蛇 3.7
psql (PostgreSQL) 13.3
$ pip freeze
asgiref==3.4.1
aws-cfn-bootstrap==2.0
Django==3.2.7
docutils==0.14
lockfile==0.11.0
psycopg2-binary==2.9.1
pystache==0.5.4
python-daemon==2.2.3
pytz==2021.1
simplejson==3.2.0
sqlparse==0.4.2
typing-extensions==3.10.0.2
我正在尝试将 PostgreSQL 与 Django 一起使用。
我改变了 setting.py 如下:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'localhost',
'PORT': '5432',
}
}
我为 PostgreSQL 创建了密码并成功登录 作为这个用户。
$ sudo su - postgres
Last failed login: Sun Sep 26 18:21:57 JST 2021 on pts/4
There were 6 failed login attempts since the last successful login.
-bash-4.2$ psql
Password for user postgres:
psql (13.3)
Type "help" for help.
postgres=# postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
dbname | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | username=CTc/postgres
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
但是,当我尝试执行此操作时:
$ python3 manage.py runserver
我收到了这个错误:
django.db.utils.OperationalError: FATAL: Ident authentication failed for user "postgres"
pg_hba.conf 首先看起来像这样:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
我是这样改的:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
但是,无论哪种方式都不起作用,仍然是同样的错误。
我不确定问题出在哪里,想知道如何解决这个问题。
【问题讨论】:
-
你的
pg_hba.conf是什么样的? -
@tink 我编辑了我的帖子并添加了该信息!
-
您可能已经编辑了
pg_hba.conf...不过,我不知道 Django 通过什么方式与 postgres 对话。您在本地连接可能通过套接字(对等)工作,而 Django 使用 tcp 到 localhost?在这种情况下,问题是您如何为用户设置密码-您是否“更改 .... WITH ENCRYPTED PASSWORD”? -
@tink 我想我是通过 'CREATE USER USERNAME WITH PASSWORD 'PASSWORD';' 设置密码的
-
现在这可以解释为什么 MD5 失败了...顺便说一句,您在更改后是否重新启动了 postgres?
标签: django linux postgresql amazon-ec2 pycharm