I'm now learning Django and am following the Django book step by step.
When I proceeded to chapter 5 to connect my Django project to my database, I can't get it to connect. I get the error:
FATAL: password authentication failed for user "postgres"
Here's the code:
settings.py configuration:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'postgis', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': 'XXXXXXX', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '8904', # Set to empty string for default. Not used with sqlite3.
}
}
But when I'm testing the connection and settings in Python shell with these codes:
from django.db import connection
cursor=connection.cursor()
the shell replies with the error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "E:\Python27\lib\site-packages\django-1.4.1- py2.7.egg\django\db\backends\__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "E:\Python27\lib\site-packages\django-1.4.1-py2.7.egg\django\db\backends\
postgresql_psycopg2\base.py", line 177, in _cursor
self.connection = Database.connect(**conn_params)
File "E:\Python27\lib\site-packages\psycopg2\__init__.py", line 179, in connect
connection_factory=connection_factory, async=async)
OperationalError: FATAL: password authentication failed for user "postgres"
then I reinstalled the PostgreSQL and pycopg2 packages. I started pgAdminIII to run the server and connected to database, then I run the same code in python shell, but the problem still exists.
Need Help!
With PostgreSQL 14+ there's a new way to get the fe_sendauth: error sending password authentication error.
As documented in the PostgreSQL 14 release notes, both the client and server now use the OpenSSL EVP API, which enforces FIPS mode. If your passwords use md5, you will be unable to login using PostgreSQL 14 if your FIPS configuration disallows md5.
You can check your default password_encryption setting using SHOW password_encryption;, and update a single user manually with:
SET password_encryption = 'scram-sha-256';
ALTER USER "user" with password 'password';
To permanently change the default password_encryption setting for new users' passwords, update postgresql.conf:
password_encryption = scram-sha-256
From the PostgreSQL 14 release notes:
Change SHA1, SHA2, and MD5 hash computations to use the OpenSSL EVP API (Michael Paquier)
This is more modern and supports FIPS mode.
Change the default of the password_encryption server parameter to
scram-sha-256(Peter Eisentraut)Previously it was
md5. All new passwords will be stored as SHA256 unless this server setting is changed or the password is specified in MD5 format. Also, the legacy (and undocumented) Boolean-like values which were previously synonyms formd5are no longer accepted.
At a guess, you're using the wrong password, or your pg_hba.conf isn't correctly configured.
Can you connect using psql with the desired credentials? Try:
psql -U postgres -W -p 8904 postgis
If not, check pg_hba.conf to see the postgres user is using ident or md5 security. If you want to use password connections it needs to be md5. If you change the setting, use pg_ctl reload or just restart PostgreSQL to make the change take effect.
You can find pg_hba.conf by connecting to your DB with psql and running SHOW hba_file;. Eg:
sudo -u postgres psql -c 'SHOW hba_file;'
Note that you should never user a PostgreSQL superuser for an application connection anyway. Create a new user and have Django use that user on a new database. For example, to create a user named django and a database named django_db that's owned by the django user:
sudo -u postgres createuser -SDR -P django
sudo -u postgres createdb -O django_db django;
The -SDR makes the default explicit, that the django user should not be a superuser and should not have CREATEDB or CREATEUSER rights.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With