How to change PostgreSQL user password  via the command line on Linux?

How to change PostgreSQL user password via the command line on Linux?


1- Check if it's installed

Just to make sure that Postgresql is already installed.

which psql

You should get something like this


2- Check the version (Optional)

psql --version

or

psql --version

You should get something like this


3- Check PostgresQL service

sudo service postgresql status

You should get something like this

postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Wed 2023-08-02 07:38:29 -03; 4h 56min ago
    Process: 1489 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 1489 (code=exited, status=0/SUCCESS)
        CPU: 1ms


4- Restart PostgresQL service

The following command is going to start the PostgresQL service:

sudo service postgresql start

The following command is going to restart the PostgresQL service.

sudo service postgresql restart

5- Enter the the PostgresQL terminal

sudo -u postgres psql

You should be able to see something like this:


6 - Check the roles/users

In order to check the current/available roles:

\du

You should be able to see something like the following:

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 calaca    | Superuser, Create role, Create DB                          | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=#

In my example, my role name is calaca.


7- Change the password

ALTER USER calaca WITH PASSWORD 'yourpassword';

After the command, you should be able to see something like that:

postgres=# ALTER USER calaca WITH PASSWORD 'computer';
ALTER ROLE
postgres=#


8- Exit the PostgreSQL terminal

\q

9- Test the new password

After changing the password for a PostgreSQL user, the user can test the new password by attempting to connect to the PostgreSQL database using the updated credentials.

Here's the template of how the user can test the new password:

psql -U your_username -d your_database_name -h your_host -W

In my example, this is how is going to become:

 psql -U calaca -d depot_development -h localhost -W

Due to the use of -W, after running the command, you should be promptly required to enter the password:

calaca@calaca-PC ~ $ psql -U calaca -d depot_development -h localhost -W
Password:

If your password is correct, you'll be able to enter the PostgreSQL terminal.


Celebrate

You've made it!

The Office GIF - The Office Happy - Discover & Share GIFs


Let's become friends


Final thoughts

I hope this article helped you. Let me know if you have any questions. Your thoughts, suggestions and corrections are more than welcome.

By the way, feel free to drop your suggestions on new blog articles.

Hope to see you next time.