
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 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:
- Run
sudo -u postgres psqlto connect without a password (peer authentication). - Inside psql, run
\password postgresand 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
trustmethod lets anyone connect with no password at all. Only use it for the few seconds it takes to reset, and always restoremd5orscram-sha-256immediately 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.



