How to Change the Password for a Postgres User

Tutorial on how to change the password for a postgres user.

This post contains steps to change your password in postgres. It requires that you know your current password. If you’ve lost your password and need to reset it, jump to this section .

Step 1: Log into Postgres

Open up a terminal, and run the following command to log into psql:

sudo -u myusername psql

Step 2: Change the password for user

Now that you’re logged ino psql, you can change your password by running the following command:

\password postgres

After you run that command, you’ll get a prompt asking you to enter a new password. Enter your new password and then quit out of psql by running:


Step 3: Restart postgres

After you perform the password change, you’ll want to restart the postgres server. From the terminal, run:

sudo service postgresql restart

Password change complete!

That’s it! You’ve successfully changed your postgres password for a particular user.

As a bonus, you did it in a secure way. The password won’t be stored in your terminal history since you did it directly in psql.

Changing a Postgres User Password if You’ve Lost the Current Password

If you’ve lost your current password for a user, don’t worry. As long as you have sudo privileges on the machine, you’ll be able to change it.

Here’s how:

Step 1: Log in without a password

You’ll need to login to psql without a password. Type the following command in your terminal:

sudo -u your_user_name psql database_name

Step 2: Reset the password

To reset the forgotten password, run the following command in psql:

ALTER USER your_user_name WITH PASSWORD 'some-new-password';

This will set the password for the your_user_name user to some-new-password.

Password reset complete!

Success. You now have reset the password for a postgres user account.

For other Postgres examples: