• Tutorials
  • PostgreSQL
  • Security

How to Safely Change the Postgres User Password via PSQL

The safest way to set or change the postgres superuser password: connect locally with psql, use \password, and keep the superuser out of your applications.

By ZeroTwo Solutions2 min read

One of the most common questions we hear from new PostgreSQL users is: “How do I safely set or change the password for the postgres user?” If you are just starting with PostgreSQL, welcome. You are in for a powerful, flexible database. This short guide shows the safest way to do it and the habits that keep your superuser account out of trouble.

Quick intro: the postgres user

The postgres user is the default superuser of a PostgreSQL cluster. We recommend using this account only to manage global objects (roles, tablespaces and databases) and to perform system-level administration. Never use the postgres superuser for your everyday applications. It is like using the root account to browse the web.

Our preferred connection method

As a console-first team, we like to become the postgres system user with sudo, then connect locally with psql. On most Linux installations this uses peer authentication over a Unix socket, which avoids network-related security issues entirely.

sudo -i -u postgres
psql

The safest way to set the postgres password

The most secure method is to connect locally with psql and run the \password meta-command. You are prompted to enter and confirm the new password, and nothing is left behind in logs or history.

postgres=# \password
Enter new password:
Enter it again:
postgres=#

To change the password of another role, pass its name: \password app_user.

Why this method is best

  • No password exposure in logs. psql hashes the password on the client before sending it, so the server never logs the plain text.
  • No SQL history with your password. Nothing sensitive ends up in ~/.psql_history.
  • No config files involved. You do not need to edit anything to change a password.
  • Quick and secure. It takes seconds and works on every supported PostgreSQL version.

Compare that with running ALTER USER postgres PASSWORD '...' directly. That statement contains the password in plain text, so it can end up in your shell or psql history and, depending on your logging settings, in the server log.

Never use the postgres user for applications

Always create dedicated roles for your applications. PostgreSQL’s role-based access system lets you assign fine-grained privileges, organize users into groups and separate administration from daily operations.

CREATE ROLE app_user LOGIN;
GRANT CONNECT ON DATABASE app_db TO app_user;

Then set that role’s password the same safe way, with \password app_user, and grant only the privileges the application actually needs.

Bonus tip: check pg_hba.conf

After changing your password, review your pg_hba.conf file to make sure the authentication method you want is configured for each connection type. Prefer scram-sha-256 over the older md5 method, and make sure password_encryption is also set to scram-sha-256 so new passwords are stored with the stronger hash. Reload the configuration after any change:

SELECT pg_reload_conf();

More articles

All articles
Start a project

Have an AI or FinTech product to build?

Tell us what you want to ship. You will get a technical roadmap and a fixed-scope proposal within 48 hours.

  • Reply within 1 business day
  • NDA on request
  • You own 100% of the IP