PostgreSQL: FATAL: password authentication failed for user
Educational use only. Content explains errors and defensive fixes for systems you own or are authorised to test. Do not use any technique here to access data, accounts, or networks without permission.
Root Cause
Similar to MySQL's access denied error, this indicates PostgreSQL rejected the connection due to invalid credentials. However, Postgres has a highly configurable authentication system managed by the `pg_hba.conf` file. Even with the correct password, authentication can fail if the `pg_hba.conf` file is configured to reject connections from your IP, or if it requires a different authentication method (like `peer` or `ident`) instead of `md5` or `scram-sha-256` for your specific connection type (local socket vs TCP/IP).
Fix / Solution
First, confirm your username and password. If correct, locate the `pg_hba.conf` file (usually in `/etc/postgresql/XX/main/` or `/var/lib/pgsql/data/`). Ensure there is a rule allowing your connection (e.g., `host all all 0.0.0.0/0 md5` for remote access with a password). After modifying `pg_hba.conf`, you must restart or reload the PostgreSQL service for the changes to take effect.
Code Snippet
# ❌ FATAL: password authentication failed
psql -h localhost -U myuser -d mydb
# ✅ Edit pg_hba.conf to allow local TCP/IP connections with password
# TYPE DATABASE USER ADDRESS METHOD
host all all 127.0.0.1/32 scram-sha-256
# ✅ Reload Postgres configuration
sudo systemctl reload postgresql