Introduction
At the time the command execution of ‘python manage.py runserver in the command line, it triggers an error message. The error message available as part of the title of the article. In the complete version, the following is the actual error appear at the command execution :
(env) C:\programming\python\django\myproject>python manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). Exception in thread django-main-thread: Traceback (most recent call last): File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection self.connect() File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner return func(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 200, in connect self.connection = self.get_new_connection(conn_params) File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner return func(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection connection = Database.connect(**conn_params) File "C:\app\python39\lib\site-packages\psycopg2\__init__.py", line 122, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: FATAL: password authentication failed for user "django_postgres" The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\app\python39\lib\threading.py", line 973, in _bootstrap_inner self.run() File "C:\app\python39\lib\threading.py", line 910, in run self._target(*self._args, **self._kwargs) File "C:\app\python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper fn(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run self.check_migrations() File "C:\app\python39\lib\site-packages\django\core\management\base.py", line 486, in check_migrations executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) File "C:\app\python39\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__ self.loader = MigrationLoader(self.connection) File "C:\app\python39\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__ self.build_graph() File "C:\app\python39\lib\site-packages\django\db\migrations\loader.py", line 220, in build_graph self.applied_migrations = recorder.applied_migrations() File "C:\app\python39\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations if self.has_table(): File "C:\app\python39\lib\site-packages\django\db\migrations\recorder.py", line 55, in has_table with self.connection.cursor() as cursor: File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner return func(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor return self._cursor() File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 235, in _cursor self.ensure_connection() File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner return func(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection self.connect() File "C:\app\python39\lib\site-packages\django\db\utils.py", line 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection self.connect() File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner return func(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\db\backends\base\base.py", line 200, in connect self.connection = self.get_new_connection(conn_params) File "C:\app\python39\lib\site-packages\django\utils\asyncio.py", line 26, in inner return func(*args, **kwargs) File "C:\app\python39\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection connection = Database.connect(**conn_params) File "C:\app\python39\lib\site-packages\psycopg2\__init__.py", line 122, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: FATAL: password authentication failed for user "django_postgres"
Actually, this article has a connection with several previous articles. The first one which is the article where in the article itself containing the base application used in this article. Upon executing or running that application, the above error message appear. The above error message has several condition which is triggering it. There are several conditions which are in the following details triggering the error message. First of all, there is a database configuration connection exist in ‘settings.py’ file. It is a file responsible as the Django configuration settings. The following is the content of the file representing the database configuration connection :
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'USER': 'django_postgres', 'NAME': 'sysapp', 'PASSWORD': 'password', } }
Solution
So, according to the information available in the introduction part, the solution for solving the error message appear is very simple. The reason is because of there is an authentication process failed. So, the following are the steps for solving the problem :
-
Try to access the database using the account available in the database configuration connection exist in ‘settings.py’ file. The following is an attempt to do it :
C:\>psql -Udjango_postgres -d sysapp Password for user django_postgres: psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "django_postgres" C:\>
-
Apparently, according to the above output, the process ends in a failure. The username and password available in the ‘settings.py’ file cannot be used to connect to the database. In this case, try to check whether the user is exist or not. Do that by logging in to the PostgreSQL database server using root or admin account as follows :
C:\>psql -Upostgres Password for user postgres: psql (14.0) WARNING: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly. See psql reference page "Notes for Windows users" for details. Type "help" for help. postgres=# \du List of roles Role name | Attributes | Member of -----------------+------------------------------------------------------------+----------- django_postgres | | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} postgres=#
-
As in the above output of the command execution, the user account actually exist. So, in that case, the user is available but the password is wrong. In the end, it is failing the authentication process to the database server. So, just try to change the user by executing the following query or command :
postgres=# alter user django_postgres with encrypted password 'password'; ALTER ROLE postgres=#
-
After successfully altering or changing the password, try to login again using that username and password as follows :
postgres=# \q C:\>psql -Udjango_postgres -d sysapp Password for user django_postgres: psql (14.0) WARNING: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly. See psql reference page "Notes for Windows users" for details. Type "help" for help. sysapp=>
-
Since the login process using the username and password as in the ‘settings.py’ file is a success after changing the password, just run the application once more. So, if there are no further error, the application will run properly.