Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Change a PostgreSQL Password from the Command Line

This guide shows you how to set a new password for a PostgreSQL user on a Linux server. It needs root access, so it fits a VPS or dedicated server.

How-To Guides2 min read7 steps

What is PostgreSQL?

PostgreSQL is a database program. A database stores information in an organised way. Each person or app that connects to it uses a user name and a password. Root is the all-powerful admin account on Linux. You need it, or the ability to use sudo, for this guide. Sudo means "run this command as the admin".

Warning: Apps use this password to reach the database. If you change it, update the password in your app's settings too, or your site may stop working. Write the new password down safely.

Steps

  1. Log in to your server with SSH. SSH is a safe way to type commands on a server from far away.
  2. Switch to the postgres account. It is the main PostgreSQL admin user.
    sudo -i -u postgres
  3. Open the PostgreSQL command tool, called psql.
    psql
    Your prompt will change to postgres=#.
  4. Run this command. Replace myuser and the password with your own.
    ALTER USER myuser WITH PASSWORD 'NewStrongPassword';
    Keep the single quotes and the semicolon.
  5. Look for the answer ALTER ROLE. This means it worked.
  6. Leave psql.
    \q
  7. Go back to your normal user.
    exit

A shortcut

You can also type \password myuser inside psql. It asks you to type the new password twice and hides what you type. This keeps the password out of your command history.

If you forgot the admin password

You do not need the old password when you use the postgres system account as shown above. Most setups let that account in without a password. If yours does not, ask Hostvento support for help.

Choose a good password

  • Use at least 12 characters.
  • Mix letters, numbers and symbols.
  • Do not reuse a password from another service.

Quick recap

  • Log in with SSH and switch to the postgres user.
  • Open psql.
  • Run ALTER USER name WITH PASSWORD 'new';
  • Update the password in your app too.