Introduction
Another article containing an attempt for solving an error message appear upon the execution of migration process in Django application. So, what is the error message ?. Actually, the error message itself appear as part of the title of the article. The following are the actual execution of the database migration process in Django application :
(env) [django@localhost project]$ python manage.py migrate /home/django/sites/env/lib64/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>. """) Traceback (most recent call last): File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection self.connect() File "/home/django/sites/env/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 200, in connect self.connection = self.get_new_connection(conn_params) File "/home/django/sites/env/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection connection = Database.connect(**conn_params) File "/home/django/sites/env/lib64/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: fe_sendauth: no password supplied The above exception was the direct cause of the following exception: Traceback (most recent call last): File "manage.py", line 22, in <module> main() File "manage.py", line 19, in main execute_from_command_line(sys.argv) File "/home/django/sites/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line utility.execute() File "/home/django/sites/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 413, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/django/sites/env/lib/python3.6/site-packages/django/core/management/base.py", line 354, in run_from_argv self.execute(*args, **cmd_options) File "/home/django/sites/env/lib/python3.6/site-packages/django/core/management/base.py", line 398, in execute output = self.handle(*args, **options) File "/home/django/sites/env/lib/python3.6/site-packages/django/core/management/base.py", line 89, in wrapped res = handle_func(*args, **kwargs) File "/home/django/sites/env/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 75, in handle self.check(databases=[database]) File "/home/django/sites/env/lib/python3.6/site-packages/django/core/management/base.py", line 423, in check databases=databases, File "/home/django/sites/env/lib/python3.6/site-packages/django/core/checks/registry.py", line 76, in run_checks new_errors = check(app_configs=app_configs, databases=databases) File "/home/django/sites/env/lib/python3.6/site-packages/django/core/checks/model_checks.py", line 34, in check_all_models errors.extend(model.check(**kwargs)) File "/home/django/sites/env/lib/python3.6/site-packages/django/db/models/base.py", line 1290, in check *cls._check_indexes(databases), File "/home/django/sites/env/lib/python3.6/site-packages/django/db/models/base.py", line 1682, in _check_indexes connection.features.supports_covering_indexes or File "/home/django/sites/env/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/postgresql/features.py", line 93, in is_postgresql_11 return self.connection.pg_version >= 110000 File "/home/django/sites/env/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 329, in pg_version with self.temporary_connection(): File "/usr/lib64/python3.6/contextlib.py", line 81, in __enter__ return next(self.gen) File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 603, in temporary_connection with self.cursor() as cursor: File "/home/django/sites/env/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 259, in cursor return self._cursor() File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 235, in _cursor self.ensure_connection() File "/home/django/sites/env/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection self.connect() File "/home/django/sites/env/lib/python3.6/site-packages/django/db/utils.py", line 90, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection self.connect() File "/home/django/sites/env/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 200, in connect self.connection = self.get_new_connection(conn_params) File "/home/django/sites/env/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner return func(*args, **kwargs) File "/home/django/sites/env/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection connection = Database.connect(**conn_params) File "/home/django/sites/env/lib64/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: fe_sendauth: no password supplied (env) [django@localhost ~]$
Before going on to the solution process, for a specific information in migrating database, this Django application is using PostgreSQL database server. If there is a need to supply a password, the password definition in the ‘settings.py’ file as part of the Django application must exist. The following is the original content of the ‘settings.py’ file :
# Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.postgresql_psycopg2', # 'NAME': 'db.sqlite3', 'NAME': 'db_apps', 'USER': 'user_apps', 'HOST': '127.0.0.1', 'PORT': '5432', } }
Moreover, for further information, the following is the PostgreSQL database version as follows :
[root@localhost ~]# psql --version psql (PostgreSQL) 14.1 [root@localhost ~]#
Solution
This is the attempt for the solution to solve the error message above. The following is the sequence for finding the solution as an attempt :
-
Actually, the above configuration is not using a password for PostgreSQL database connection because of the PostgreSQL database configuration connection settings. By default, as it exist in ‘/var/lib/pgsql14/data/pg_hba.conf’, the following is the default PostgreSQL database configuration connection for local user :
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer
-
But it seems, Django application does not allow any connection to the PostgreSQL database connection without supplying any password. So, change the PostgreSQL database connection configuration by changing the authentication method from ‘peer’ to ‘scram-sha-256’ as the default authentication method as follows :
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only #local all all peer local all all scram-sha-256
-
Following after, just change the ‘settings.py’ file of the Django application as follows :
# Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.postgresql_psycopg2', # 'NAME': 'db.sqlite3', 'NAME': 'db_apps', 'USER': 'user_apps', 'PASSWORD' : 'password', 'HOST': '127.0.0.1', 'PORT': '5432', } }
-
Do not forget to change the password for the user ‘user_apps’ by executing the following command :
[root@localhost ~]# psql -Upostgres Password for user postgres: psql (14.1) Type "help" for help. postgres=# alter user user_apps with encrypted password 'password'; ALTER ROLE postgres=# \q [root@localhost ~]#
-
Last but not least, just test the user whether it can access the database application using the updated password :
[root@localhost ~]# psql -Uuser_apps -d db_apps Password for user user_apps: psql (14.1) Type "help" for help. postgres=>
-
Finally, just run the migration command for the Django application which is ‘python manage.py migrate’ as in above or the previous part.
One thought on “How to Solve Error Message django.db.utils.OperationalError: fe_sendauth: no password supplied when migration database Django Application”