How to Change PostgreSQL User Password
In this tutorial, you'll learn about How to Change PostgreSQL User Password. We cover key concepts, practical examples, and best practices.
The Problem
You need to change a PostgreSQL user password due to a security policy, a compromised credential, or an onboarding process. Running psql -U myuser fails with FATAL: password authentication failed for user and you need to reset it.
Quick Fix
Step 1: Connect as a superuser
Log in with a PostgreSQL superuser (usually postgres):
sudo -u postgres psql
psql (16.0)
Type "help" for help.
postgres=#
Step 2: Change the password with ALTER USER
Set a new password for a specific user:
ALTER USER myuser WITH PASSWORD 'NewStrongPassword123!';
ALTER ROLE
Step 3: Use the \password command inside psql
An alternative that hashes the password automatically:
sudo -u postgres psql
Then:
postgres=# \password myuser
Enter new password:
Enter it again:
Step 4: Update the password for the postgres superuser
Change the postgres superuser password:
ALTER USER postgres WITH PASSWORD 'SuperSecurePassword456!';
Step 5: Update the pg_hba.conf if needed
If password changes still fail, check the authentication method:
sudo grep -n "local\|host" /etc/postgresql/16/main/pg_hba.conf | head -10
local all all peer
host all all 127.0.0.1/32 scram-sha-256
If local uses peer, you must connect via Unix socket as the database user. Change to md5 or scram-sha-256 for password-based local connections.
Step 6: Test the new password
Exit psql and test the updated credentials:
psql -U myuser -d my_database -W
Enter the new password when prompted.
Alternative Solutions
Use SCRAM-SHA-256 for stronger password hashing
Ensure pg_hba.conf uses scram-sha-256:
host all all 0.0.0.0/0 scram-sha-256
Set password expiration policies
Force users to rotate passwords periodically:
ALTER ROLE myuser VALID UNTIL '2026-09-24';
Common Mistakes to Avoid
Changing the password without updating application config files. After resetting the password, all applications that connect to PostgreSQL need their connection strings updated.
Using md5 when scram-sha-256 is expected. Newer PostgreSQL versions default to SCRAM. Check pg_hba.conf to ensure the auth method matches.
Not testing the new password before closing the session. Keep your superuser session open while testing the new password in another terminal.
Pro Tips
Use SCRAM-SHA-256 for password encryption. Ensure password_encryption = scram-sha-256 in <a href="/databases/postgresql/">postgresql</a>.conf for the strongest built-in password hashing.
Set password expiration for service accounts. Use ALTER ROLE myuser VALID UNTIL '2026-12-31' to enforce periodic password rotation.
Use pgp_sym_encrypt for application-level encryption. For sensitive data, encrypt at the application level in addition to database authentication.
Use psql's \password command for hashed passwords. The \password meta-command automatically hashes the password using the server's current encryption setting, avoiding plain-text transmission over the connection.
Prevention
- Use strong passwords with a mix of uppercase, lowercase, numbers, and special characters.
- Store database passwords in a password manager or secrets vault.
- Rotate passwords periodically and after any team member with access leaves.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro