【问题标题】:authentication failed when I use postgresql with django当我将 postgresql 与 django 一起使用时,身份验证失败
【发布时间】: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


【解决方案1】:

您的数据库名称是“postgres”,您可以在 psql bash (postgres=#) 上看到,但您在设置中将其设置为“dbname”

【讨论】:

  • 我也做了一个名为“dbname”的数据库。
  • 您是否尝试过更改 pg_hba.conf 文件的身份验证方法?将“Ident”更改为“md5”?像这样:stackoverflow.com/a/64596782/13118713
  • 正如我在上面添加的,我改变了它们。
【解决方案2】:

您被配置为使用ident 身份验证,而不是密码。为用户设置密码并不会改变这一点——分配的密码将只是闲置而未使用。如果要使用密码认证,则需要更改 pg_hba.conf 以指定它,md5"scram-sha-256"

或者,如果您的框架希望您这样做,您可以学习如何正确使用 ident

【讨论】:

  • 我更改了 pg_hba.conf,但它不起作用:(我添加了我的 pg_hba.conf,你能检查一下吗?
【解决方案3】:

我通过“ALTER .... WITH ENCRYPTED PASSWORD”为用户设置密码? 然后问题就解决了!

【讨论】:

    猜你喜欢
    • 2011-12-31
    • 2022-01-11
    • 2018-11-02
    • 2015-06-18
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2014-07-15
    • 2014-11-03
    相关资源
    最近更新 更多