当我将 postgresql 与 django 一起使用时身份验证失败
authentication failed when I use postgresql with django
我的环境
OS:AWS linux
python3.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
然而,这两种方法都不起作用,仍然是同样的错误。
我不确定问题出在哪里,想知道如何解决这个问题。
您的数据库名称是“postgres”,您可以在 psql bash (postgres=#) 上看到,但您在设置中将其设置为“dbname”
您配置为使用 ident
身份验证,而不是密码。为用户设置密码不会改变这一点——分配的密码只会放在那里未被使用。如果要使用密码认证,需要改pg_hba.conf指定,md5
或"scram-sha-256"
。
或者,如果您的框架希望您这样做,您可以学习如何正确使用 ident
。
我通过'ALTER .... WITH ENCRYPTED PASSWORD'为用户设置了密码?
然后问题就解决了!
我的环境
OS:AWS linux
python3.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
然而,这两种方法都不起作用,仍然是同样的错误。
我不确定问题出在哪里,想知道如何解决这个问题。
您的数据库名称是“postgres”,您可以在 psql bash (postgres=#) 上看到,但您在设置中将其设置为“dbname”
您配置为使用 ident
身份验证,而不是密码。为用户设置密码不会改变这一点——分配的密码只会放在那里未被使用。如果要使用密码认证,需要改pg_hba.conf指定,md5
或"scram-sha-256"
。
或者,如果您的框架希望您这样做,您可以学习如何正确使用 ident
。
我通过'ALTER .... WITH ENCRYPTED PASSWORD'为用户设置了密码? 然后问题就解决了!