How to Fix PostgreSQL Password Authentication Failed for User Error
In this tutorial, you'll learn about How to Fix PostgreSQL Password Authentication Failed for User Error. We cover key concepts, practical examples, and best practices.
The Problem
You try to connect to PostgreSQL and get:
FATAL: password authentication failed for user "myuser"
Or:
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "myuser"
PostgreSQL rejected the password you provided. The password may be wrong, the authentication method may be wrong, or the password may have expired.
Quick Fix
1. Reset the user password
Connect as the postgres superuser and set a new password:
sudo -u postgres psql
ALTER USER myuser WITH PASSWORD 'new_secure_password';
Test immediately:
psql -U myuser -d mydb -h localhost -W
2. Check pg_hba.conf authentication method
The pg_hba.conf file may require an authentication method that does not match the client:
sudo nano /etc/postgresql/16/main/pg_hba.conf
For local connections using a password:
# Wrong — peer requires OS user match
local all all peer
# Right — md5 or scram-sha-256 for password
local all all scram-sha-256
For remote connections:
# Wrong — trust allows anyone
host all all 0.0.0.0/0 trust
# Right — require password
host all all 0.0.0.0/0 scram-sha-256
Reload after editing:
sudo systemctl reload postgresql
3. Check the connection password encoding
Special characters in passwords must be URL-encoded in connection strings:
# Wrong — special chars not encoded
postgresql://myuser:p@ssw0rd@localhost:5432/mydb
# Right — @ encoded as %40
postgresql://myuser:p%40ssw0rd@localhost:5432/mydb
4. Check for password expiration
PostgreSQL passwords can expire:
SELECT rolname, rolvaliduntil FROM pg_roles WHERE rolname = 'myuser';
If expired, set a new validity:
ALTER USER myuser WITH PASSWORD 'new_password' VALID UNTIL 'infinity';
5. Verify the user exists and can log in
SELECT rolname, rolcanlogin, rolconnlimit FROM pg_roles
WHERE rolname = 'myuser';
If rolcanlogin is false:
ALTER ROLE myuser WITH LOGIN;
Prevention
- Always use
scram-sha-256authentication for production databases. - Use a password manager to generate and store database passwords.
- Set
VALID UNTIL 'infinity'for application users to prevent expiration. - Test password authentication after every user creation or password change.
Common Mistakes with password auth
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world POSTGRES code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro