Introduction
This article will discuss about an error message appear. The appearance of the error has a connection with the Django-based project’s execution. Upon the execution of the Django-based project, the error appear. The following is the part of the error message :
(env) C:\programming\python\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:\programming\python\env\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection self.connect() File "C:\programming\python\env\lib\site-packages\django\utils\asyncio.py", line 26, in inner return func(*args, **kwargs) File "C:\programming\python\env\lib\site-packages\django\db\backends\base\base.py", line 200, in connect self.connection = self.get_new_connection(conn_params) File "C:\programming\python\env\lib\site-packages\django\utils\asyncio.py", line 26, in inner return func(*args, **kwargs) File "C:\programming\python\env\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection connection = Database.connect(**conn_params) File "C:\programming\python\env\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 "Administrator"
Apparently, the error exist after adding a PostgreSQL database configuration connection in ‘settings.py’ file. It is a file acts as a project configuration setting and it exist in the root folder of the project. The following is the actual snippet configuration of it from the original one to the modified one.
The original one exist as follows :
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } }
The following is the modification from the above one :
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'django_postgres', 'PASSWORD': 'password', } # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': BASE_DIR / 'db.sqlite3', # } }
Solution
The cause of the error is because of an incorrect configuration settings. Precisely, the incorrect PostgreSQL database configuration connection settings. The above configuration only have the password setting available in the ‘PASSWORD’ entry. But the ‘NAME’ entry is not the database username entry. Instead, it is the name of the database which is going to be used. Basically, upon connecting to a PostgreSQL database server does not need to specify the database name. But it must have an entry or a value for the username connecting to the PostgreSQL database server. Just erase the ‘NAME’ entry and replace it with ‘USERNAME’ instead as follows :
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'USER': 'django_postgres', 'PASSWORD': 'password', } # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': BASE_DIR / 'db.sqlite3', # } }
Try to execute the Django-based application once more. If there are no further error appear, the application will run normally. Just make sure that the PostgreSQL database connection is also a success if it is run directly for an example in a command line interface as follows :
C:\Users\Personal>psql -Udjango_postgres -d postgres Password for user django_postgres: psql (12.2) 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=>