Django.db.utils.OperationalError: connection to server at "localhost" (::1), port 5432 failed: fe_sendauth: no password supplied

while trying to connect my postgresql database with django i'm facing this error django.db.utils.OperationalError: connection to server at "localhost" (::1), port 5432 failed: fe_sendauth: no password supplied

this is my settings.py

`DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('DB_NAME'),
        'USER': os.environ.get('DB_USER'),
        'PASSWORD': os.environ.get('DB_USER_PASSWORD'),
        'HOST': os.environ.get('DB_HOST'),
    }
}`

this is my .env file

`export DB_NAME=demo
export DB_USER=postgres
export DB_USER_PASSWORD=12345678
export DB_HOST=localhost`

First install the Python Decouple library by running pip install python-decouple. Then type the following in your .env file

DB_NAME=demo
DB_USER=postgres
DB_PASSWORD=12345678
DB_HOST=localhost

Ensure that these are your correct database credentials. In your settings.py, import the library:

from decouple import config

then retrieve the settings parameters:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': config('DB_NAME'),
        'USER': config('DB_USER'),
        'PASSWORD': config('DB_PASSWORD'),
        'HOST': config('DB_HOST'),
        'PORT': '',
    }
}

See https://simpleisbetterthancomplex.com/2015/11/26/package-of-the-week-python-decouple.html

Back to Top