Change PostgreSQL User Password: Secure Your Database Access

How to change or reset a PostgreSQL password

Whether you’re rotating credentials as good practice or you’ve locked yourself out of a database you administer, changing a PostgreSQL user password is a one-line job — once you know which line. Forgetting the postgres superuser password is a bit more involved, but still recoverable on a server you control. We at GetMyPassword cover both, including the trick that gets you back in when you can’t log in at all.

Change a PostgreSQL password
Changing a PostgreSQL user password from psql.

Change a password with ALTER USER

If you can already connect as a superuser, this is all it takes. Open psql and run:

ALTER USER username WITH PASSWORD 'new_password';

Even cleaner is the \password meta-command — type \password username and psql prompts you to enter the new password twice, hidden from the screen and from your shell history. You’ll need superuser or alter-role privileges to change another user’s password.

Reset a forgotten postgres password

Locked out of the postgres superuser itself? On Linux, the default install trusts the system postgres user, so the quickest route is:

  1. Run sudo -u postgres psql to connect without a password (peer authentication).
  2. Inside psql, run \password postgres and set a new password.

If that’s not available, the fallback is to edit pg_hba.conf, temporarily change the auth method from md5 (or scram-sha-256) to trust, reload PostgreSQL, connect, run the ALTER USER command above — then change pg_hba.conf back and reload again.

The trust method lets anyone connect with no password at all. Only use it for the few seconds it takes to reset, and always restore md5 or scram-sha-256 immediately afterwards.

Optional: make the password expire

For tighter control you can attach an expiry date with the VALID UNTIL clause — ALTER USER username VALID UNTIL '2027-01-01'; — or set it to 'infinity' for a password that never expires. Handy for temporary access granted to a contractor.

Choose a password worthy of the database

A database superuser is among the most powerful credentials on a server, so make the new one long and random rather than memorable. Generate one with our password generator and store it securely. If you also run MySQL, the same principle applies — see our guide to changing the MySQL root password.

Frequently asked questions

How do I change a PostgreSQL user password?

Connect with psql as a superuser and run ALTER USER username WITH PASSWORD ‘new_password’; or use the \password username command, which prompts you to type the new password securely.

How do I reset a forgotten postgres password?

On Linux, run sudo -u postgres psql to connect via peer authentication, then \password postgres. If that fails, temporarily set trust in pg_hba.conf, reset with ALTER USER, and revert the file.

Do I need superuser rights to change a password?

To change another user’s password, yes — you need superuser or alter-role privileges. Any user can change their own password with the \password command.

Help your friends stay safe. Share this article!