Get Started Free

How to change a user's password in PostgreSQL

Change a PostgreSQL password with \password or ALTER USER, keep it out of logs and history, and reset the postgres superuser password if you're locked out.

Updated November 30, 2026 ยท Tested on PostgreSQL 16.14, psql 16.14

Quick Answer

In psql, run \password with the user name. It prompts twice and never shows the password:

postgres=# \password app_user
Enter new password for user "app_user":
Enter it again:

From any other client: ALTER USER app_user WITH PASSWORD 'new-password';

\password vs ALTER USER

Both change the same thing. The difference is where the plain-text password ends up:

MethodPlain text visible inUse it for
\password app_userNowhere. psql hashes it locally and sends only the hash (shown below).Interactive changes. The safest option.
ALTER USER ... PASSWORD '...'Your psql history file, and the server log if statement logging is onScripts and non-psql clients

\password with no name changes the password of the user you are connected as.

ALTER USER app_user WITH PASSWORD 'new-password';
-- ALTER ROLE does exactly the same thing
ALTER ROLE app_user WITH PASSWORD 'new-password';

If you use ALTER USER in psql, the statement is saved to ~/.psql_history. With log_statement = 'all' or 'ddl', it is also written to the server log in plain text. Prefer \password whenever you have a terminal.

In VisuaLeaf

Change a password in VisuaLeaf

VisuaLeaf's User Management lists every login role on the server. Expand a user to get an inline Change password form. The same row has a Lock button that stops a role logging in without dropping it or its grants, and the edit dialog has a valid-until field for expiring a password. If your account can't manage users, an amber banner says so before you try.

One thing to know: VisuaLeaf's docs describe the change as going through the driver's native mechanism, for example ALTER USER ... PASSWORD. If that's what reaches your server and log_statement records DDL, the logging caveat from this guide applies: the new password can end up in the server log, just as when you type the statement yourself. In psql, \password avoids this by sending only a hash. See the SQL User Management docs.

VisuaLeaf SQL User Management Users tab with a search box, a Create User button and a PostgreSQL user marked SUPER with edit, roles, lock and delete buttons
Each user row has edit, roles, lock and delete actions.

Change your own password

Any user can change their own password without special privileges:

\password

Changing another user's password needs superuser or CREATEROLE. On PostgreSQL 16 and later, a CREATEROLE user can only manage roles it has ADMIN OPTION on, which includes roles it created itself.

Expire a password or remove it

-- password stops working at midnight UTC on this date
ALTER USER app_user VALID UNTIL '2027-01-01';

-- never expires again
ALTER USER app_user VALID UNTIL 'infinity';

-- remove the password entirely (password logins will fail)
ALTER USER app_user WITH PASSWORD NULL;

VALID UNTIL applies only to password authentication. It doesn't stop a user who connects with certificate, peer or trust authentication (see the test below).

What we found testing this

We turned on log_statement = 'all' on PostgreSQL 16, changed the same password both ways, and read the server log:

LOG:  statement: ALTER ROLE app_user PASSWORD 'Plaintext-Visible-123';
LOG:  statement: ALTER USER app_user PASSWORD 'SCRAM-SHA-256$4096:r/wWdp+Y9rOiIbujnKic6A==$EONlU+ZR...'
  • \password is just an ALTER USER with the hash already computed. psql builds the SCRAM verifier on your machine, so the log, pg_stat_activity and any proxy only ever see the hash.
  • An expired password looks like a wrong password to the client. After VALID UNTIL '2000-01-01', the correct password returns the usual FATAL: password authentication failed for user "app_user". Only the server log says why: DETAIL: User "app_user" has an expired password. The same happens after PASSWORD NULL, logged as has no password assigned.
  • Expiry doesn't lock the account. The same expired user still connected over the local socket, because that pg_hba.conf line used trust. Expiry and PASSWORD NULL only block connections whose line uses scram-sha-256, md5 or password. To really lock a user out, use ALTER ROLE app_user NOLOGIN. That blocked the socket login too, with FATAL: role "app_user" is not permitted to log in.

Reset the postgres password when you are locked out

If you've forgotten the superuser password, use the OS account to get in without one:

  1. Find pg_hba.conf. In psql it's SHOW hba_file;; on Debian/Ubuntu it is usually /etc/postgresql/16/main/pg_hba.conf.
  2. Make a backup, then change the method on the local line for postgres to peer (Linux/macOS) or, temporarily, trust.
  3. Reload the server: sudo systemctl reload postgresql or pg_ctl reload.
  4. Connect and set a new password with \password postgres.
  5. Put the original line back and reload again.
sudo -u postgres psql
postgres=# \password postgres

trust lets anyone who can reach that connection log in as any user without a password. Only use it on a local (Unix socket) line, and revert it straight away.

Check how passwords are stored

PostgreSQL 14 and later hash new passwords with SCRAM-SHA-256 by default. Older servers used MD5, and roles keep their old hash until the password is set again:

SHOW password_encryption;   -- scram-sha-256

-- superuser only: which roles still have an MD5 hash
SELECT rolname FROM pg_authid WHERE rolpassword LIKE 'md5%';

To upgrade an MD5 role, make sure password_encryption is scram-sha-256 and set the password again. Then you can switch its pg_hba.conf line from md5 to scram-sha-256.

Frequently Asked Questions

How do I change a PostgreSQL user password from the command line?

Run psql -U postgres, then \password username. To do it in one line from a script, use psql -c "ALTER USER username WITH PASSWORD '...'" and keep the script out of version control.

Is ALTER USER or ALTER ROLE correct?

Both. ALTER USER is an alias for ALTER ROLE, and both change the password the same way.

Do I need to restart PostgreSQL after changing a password?

No. The new password applies to new connections immediately. Existing connections stay open.

What is the default password for the postgres user?

There isn't one. Most installs let the postgres OS user connect through peer authentication, and you set a password yourself with \password postgres.

VisuaLeaf

Keep your PostgreSQL connections organized

VisuaLeaf's connection manager stores your local, cloud and team PostgreSQL connections in one place, with a SQL editor and a tree of databases, schemas and tables for each.