Skip to content

How to Fix PostgreSQL Authentication Failed Error

DodaTech Updated 2026-06-24 3 min read

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-256 authentication for password-based logins.
  • Never use trust authentication in production.
  • Use a password manager to generate and store database passwords.
  • Rotate database passwords regularly and update connection strings.

Common Mistakes with authentication

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. 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

### What is the difference between peer, md5, and scram-sha-256 authentication?

peer trusts the OS username (local connections only). md5 uses legacy MD5 password hashing. scram-sha-256 is the modern, secure password authentication method. Always prefer scram-sha-256 for new deployments.

How do I find the pg_hba.conf location?

Run sudo -u postgres psql -c "SHOW hba_file;". The default path on Ubuntu is /etc/<a href="/databases/postgresql/">postgresql</a>/16/main/pg_hba.conf.

Why does my application work locally but fail from another machine?

Local connections may use peer authentication (no password), while remote connections require password authentication. Ensure pg_hba.conf has a host entry with scram-sha-256 or md5 for remote IPs.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro