Skip to content

How to Fix PostgreSQL Password Authentication Failed for User Error

DodaTech Updated 2026-06-24 3 min read

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-256 authentication 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

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' 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

### What is the difference between md5 and scram-sha-256 in pg_hba.conf?

md5 uses legacy MD5 password hashing, which is less secure. scram-sha-256 uses the SCRAM-SHA-256 protocol, which is more secure and is the default in PostgreSQL 14+. Always use scram-sha-256 when possible.

Why does psql prompt for a password even when it is in the connection string?

If you use psql -U myuser -d mydb -h localhost, it will prompt for a password unless you use the PGPASSWORD environment variable or a .pgpass file. This is a security feature.

How do I set up a .pgpass file?

Create ~/.pgpass with the format host:port:database:user:password, then set permissions to 600 with chmod 600 ~/.pgpass. psql automatically reads this file for password authentication.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro