Skip to content
Advertisement

authentication failed when I use postgresql with django

My environment

OS: AWS linux
python 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

I am trying to use PostgreSQL with Django.

I changed setting.py like below:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'dbname',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

I created a password for PostgreSQL and I successfully logged in as this user.

$ 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

However, when I tried to execute this:

$ python3 manage.py runserver

I got this error:

django.db.utils.OperationalError: FATAL:  Ident authentication failed for user "postgres"

pg_hba.conf looked like this first:

# 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

I changed like this:

# 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

However, either way does not work and still same error.

I am not sure what the problem is and would like to know how I can fix this.

Advertisement

Answer

I set the password for the user by ‘ALTER …. WITH ENCRYPTED PASSWORD’ ? And then the problem was solved!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement