Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 28P01: Invalid Password Fix

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Error 28P01 means PostgreSQL rejected the password you supplied during authentication. The fastest fix is to connect as the postgres superuser (or another admin role) and reset the password for the role you are using, then reconnect with the new credentials. If you cannot log in at all, you will need local access to the server to reset it via the operating system user or a temporary trust rule in pg_hba.conf. This guide walks through both paths, plus the common reasons the error appears even when you think the password is correct.

What You'll Need

  • Shell access to the machine running PostgreSQL (or the container/cloud console where it runs).
  • The ability to run commands as the operating system user that owns the PostgreSQL service (usually postgres).
  • The name of the database role (user) that is failing to authenticate.
  • Access to pg_hba.conf if you need to temporarily change authentication rules.
  • A PostgreSQL client such as psql, or the admin console of your managed database provider.

Step-by-Step

  1. Confirm the exact role and host. The error message usually names the user and database. Note whether you are connecting over TCP (-h localhost or a remote IP) or over a Unix socket. Authentication rules differ between the two, and a password that works locally may fail over TCP.
  2. Check for typos and quoting. Passwords are case-sensitive. If you pass the password on the command line with -W or via PGPASSWORD, make sure shell quoting is not stripping characters. Special characters such as $, !, or backslashes need single quotes in most shells.
  3. Try connecting as the postgres superuser locally. On Linux, run sudo -u postgres psql. On macOS with Homebrew, run psql postgres as your own user if your install trusts local connections. If this works, you can reset the failing role's password from inside the session.
  4. Reset the password. Inside the psql session, run ALTER ROLE your_role WITH PASSWORD 'new_password'; replacing your_role and the password. Use a password that meets any password policy configured on the server. Then exit with \q.
  5. Test the new credentials. Connect explicitly: psql -h localhost -U your_role -d your_database. Enter the new password when prompted. If this succeeds, the error is resolved.
  6. If you cannot connect as any admin role, use a temporary trust rule. Open pg_hba.conf (its location is shown by SHOW hba_file; if you can still connect, or found in the data directory). Add a line near the top for local connections only, for example local all postgres trust or host all postgres 127.0.0.1/32 trust. Save the file.
  7. Reload the configuration. Run sudo systemctl reload postgresql on systemd Linux, or pg_ctl reload -D /path/to/data if you manage the server directly. Reloading does not drop existing connections.
  8. Connect and reset, then revert. Connect as postgres without a password, run the ALTER ROLE command from step 4, then remove the trust line you added and reload again. Leaving a trust rule in place is a serious security risk.
  9. For managed databases, use the provider console. Services such as Amazon RDS, Azure Database for PostgreSQL, Google Cloud SQL, and similar platforms have a reset-password or modify-user option in their web console. Use that instead of editing server files, which managed services do not expose.
  10. Reconnect from your application. Update the connection string or environment variable in your application, then restart the app or connection pool so it picks up the new password. Old pooled connections may keep failing until they are recycled.

Common Mistakes

  • Editing pg_hba.conf but forgetting to reload the server, so the change never takes effect.
  • Leaving a trust rule in place after the reset, which allows passwordless access from that address.
  • Resetting the password for the wrong role, especially when the application connects as a role with a different name than the database owner.
  • Assuming a password change applies to existing sessions. Open connections keep working with the old credentials until they reconnect.
  • Using ALTER USER with a password containing characters your shell interprets before psql sees them.
  • Forgetting that pg_hba.conf rules are matched top-down; a broad rule above your specific one can override it.
  • Changing the password in the database but not in the application's environment variables or secrets manager.

Tips

  • Use \du in psql to list roles and confirm the exact spelling of the role name.
  • Use \conninfo to see which host, port, user, and database your current session is using.
  • Set PGPASSWORD only for one-off tests; storing it in shell history or scripts is a security risk.
  • Prefer a .pgpass file with restrictive permissions for scripted connections.
  • Check the server log for the exact authentication method that failed; it often names the pg_hba.conf line number.
  • If you use connection pooling (PgBouncer, application pools), update credentials in the pooler configuration as well.

Troubleshooting

If the error persists after resetting the password, work through these checks in order. First, confirm you are connecting to the intended server and port; a second PostgreSQL instance on the same host is a common cause. Run psql -h host -p port -U role -d db explicitly rather than relying on defaults.

Next, inspect pg_hba.conf for the line that matches your connection type, address, and database. If it says peer or ident, passwords are not checked at all, and the OS user must match the database role. If it says md5 or scram-sha-256, the password is checked and must match the stored hash. A mismatch between the client's expected auth method and the server's configured method can also produce authentication failures; check the server log for details.

If you changed the password recently, verify the change took effect by connecting as that role from a fresh session. If the application still fails, check whether it caches credentials, uses a different environment file, or connects through a proxy or pooler with its own credentials.

If you cannot start or reload PostgreSQL after editing configuration, check the syntax of pg_hba.conf; a malformed line can prevent reload. Restore the previous file from backup if needed, then reload again.

If none of this works, confirm the server is reachable at the network level and that no firewall or security group is blocking the port. A connection refused error is different from 28P01, but a misrouted connection can look like an authentication problem if it reaches a different service.

Frequently Asked Questions

What does error 28P01 mean exactly?

It is PostgreSQL's SQLSTATE code for invalid password. The server received a password for the role but it did not match the stored credential, or the authentication method required a password that was not supplied correctly.

Can I reset the postgres password without logging in?

Yes, if you have OS-level access. Temporarily add a local trust rule in pg_hba.conf, reload, connect as postgres, run ALTER ROLE, then remove the rule and reload again. On managed services, use the provider's console instead.

Why does my password work in psql but fail in my application?

The application may be connecting to a different host, port, database, or role, or it may be using an old cached credential. Check the connection string and restart the app or pooler after changing the password.

Does changing a role's password affect existing connections?

No. Existing sessions stay connected until they disconnect or are terminated. New connections must use the new password.

What is the difference between peer and md5 authentication?

peer uses the operating system user name to identify the database role and does not check a password. md5 and scram-sha-256 require a password and compare it against the stored hash.

Is it safe to leave a trust rule in pg_hba.conf?

No. A trust rule allows anyone who can reach that address to connect without a password. Use it only temporarily for recovery and remove it immediately afterward.