How to Fix PostgreSQL Authentication Failed Error
In this tutorial, you'll learn about How to Fix PostgreSQL Authentication Failed Error. We cover key concepts, practical examples, and best practices.
The Problem
You try to connect to PostgreSQL and get:
psql: error: FATAL: password authentication failed for user "myuser"
Or:
FATAL: no pg_hba.conf entry for host "192.168.1.50", user "myuser", database "mydb"
PostgreSQL rejected the authentication because the password is wrong, the authentication method is misconfigured, or the user does not exist.
Quick Fix
1. Reset the user password
Connect as the postgres superuser and reset the password:
sudo -u postgres psql
ALTER USER myuser WITH PASSWORD 'new_secure_password';
Test the new password:
psql -U myuser -d mydb -h localhost -W
2. Check pg_hba.conf authentication method
The pg_hba.conf file controls which authentication method is used for each connection type. Edit it:
sudo nano /etc/postgresql/16/main/pg_hba.conf
# Wrong — 'peer' requires OS user to match DB user
local all all peer
# Right — 'md5' or 'scram-sha-256' for password auth
local all all md5
host all all 127.0.0.1/32 scram-sha-256
host all all 0.0.0.0/0 scram-sha-256
Reload the configuration:
sudo systemctl reload postgresql
3. Verify the user exists
SELECT rolname, rolcanlogin FROM pg_roles WHERE rolname = 'myuser';
If the user does not exist, create it:
CREATE USER myuser WITH PASSWORD 'secure_password' LOGIN;
If rolcanlogin is false, grant login:
ALTER ROLE myuser WITH LOGIN;
4. Check the connection string format
Ensure the connection string is correct:
# Wrong — missing password or wrong format
postgresql://myuser@localhost:5432/mydb
# Right
postgresql://myuser:password@localhost:5432/mydb
In Python with SQLAlchemy:
# Wrong — no password
engine = create_engine('postgresql://myuser@localhost/mydb')
# Right
engine = create_engine('postgresql://myuser:password@localhost/mydb')
5. Check for role inheritance
The user may inherit from another role that owns the database:
SELECT r.rolname, m.rolname AS member_of
FROM pg_roles r
JOIN pg_auth_members a ON r.oid = a.member
JOIN pg_roles m ON m.oid = a.roleid
WHERE r.rolname = 'myuser';
Grant direct access if needed:
GRANT myapp_role TO myuser;
Prevention
- Always use
scram-sha-256authentication for password-based logins. - Never use
trustauthentication in production. - Use a password manager to generate and store database passwords.
- Rotate database passwords regularly and update connection strings.
Common Mistakes with authentication
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
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