Applies to: PostgreSQL 13 to 17 on Ubuntu, Debian, AlmaLinux and Rocky Linux
You change PostgreSQL passwords from the psql shell, either with the \password command or with ALTER USER. We tested both on PostgreSQL 17: the new password worked and the old one was refused.
1. Open psql as the postgres user
sudo -u postgres psql
2a. The safe way: \password
\password app
psql asks for the new password twice, hides it while you type, and sends it to the server already encrypted, so it doesn’t end up in logs or history. Use \password postgres for the postgres user itself.
2b. With SQL
ALTER USER app WITH PASSWORD 'New-Strong-Pass-2026';
This works in scripts, but the password can appear in the server log and in ~/.psql_history.
3. Test the login
psql -h 127.0.0.1 -U app -d appdb
The old password still works, or no password is asked?
That is decided by pg_hba.conf, not the password. Lines with the method trust allow logins without a password, and peer lets a Linux user log in as the database user with the same name. In our test, the old password still “worked” over 127.0.0.1 because that address was set to trust; over the network, where the method was scram-sha-256, it was refused as expected. Find the file with sudo -u postgres psql -c "SHOW hba_file", use scram-sha-256 for password logins, then run sudo systemctl reload postgresql.
Forgot the postgres password?
On the server, sudo -u postgres psql logs in through peer authentication without a password, and \password postgres sets a new one.
Backups before bigger changes: back up and restore MySQL, MariaDB and PostgreSQL.