Change PostgreSQL User Password in Ubuntu

In this post, we will discuss how to change a PostgreSQL password in the Ubuntu operating system using the psql front-end.

First, you must sign in as the superuser postgres. In the terminal type:

sudo su - postgres

Enter your Ubuntu user password.

You are now logged in as the user postgres.

Next, you type in the terminal:

psql

This command launches the PostgreSQL's interactive terminal. 

To change a user's password, you can use the ALTER USER command.  This command changes only the mentioned attributes of a user. To change a user's password, you follow the syntax below:

ALTER USER username with PASSWORD 'password';

The username is the name of the user and the password is the new password you want to set.

For example, if you want to change the user's karina password to, let's say       
awwr124 (just some random characters), you write the following command:

ALTER USER karina with PASSWORD 'awwr124';

Another way to change a user's password is to run the \password meta-command. Meta-commands are processed by psql and sent to the server.

The \passwordcommand encrypts the password you provide and sends it to the server as an ALTER ROLE command.

The syntax is as follows:
\password username

So, if you want to change the password of the user karina you type:
\password karina
You are prompted to enter the new password and then to enter it again.

And that's it. The password was changed successfully.

Post last updated on May 24, 2022