0
votes

When I try to use the database name, username, and password from an environment variable using django-environ it shows me an error like this:

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

My settings.py:

    DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": env("DATABASE_NAME"),
        "USER": env("DATABASE_USER"),
        "PASSWORD": env("DATABASE_PASSWORD"),
        "HOST": "localhost",
        "PORT": "5432",
    }
}

It works fine when I use it without the environment variable, however.

Detailed errors:

  File "C:\Users\Dell\.virtualenvs\Blog-1bn9_xtV\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Dell\.virtualenvs\Blog-1bn9_xtV\lib\site-packages\django\db\backends\base\base.py", line 197, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Dell\.virtualenvs\Blog-1bn9_xtV\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Dell\.virtualenvs\Blog-1bn9_xtV\lib\site-packages\django\db\backends\postgresql\base.py", line 185, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\Dell\.virtualenvs\Blog-1bn9_xtV\lib\site-packages\psycopg2\__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL:  password authentication failed for user "postgres "
1
Looks like there is an extra space in the username. Are you sure you have set it correctly (and that it should be postgres instead of postgres )? - Willem Van Onsem
Thnx a lot bro, you saved me. Here i used unexpected spaces in variable. But it's ok now..! - rabiulrafi01

1 Answers

0
votes

Check for data types. Can you send print(DATABASE) with and without using env variables