# 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.

```ruby
which psql
```

You should get something like this

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672512349051/a6ac651c-dc7c-4538-b2c8-028441d63358.png align="center")

---

### 2- Check the version (Optional)

```ruby
psql --version
```

or

```ruby
psql --version
```

You should get something like this

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672514130896/93846311-c6aa-40c9-9f82-5ddb69e26763.png align="center")

---

### 3- Check PostgresQL service

```ruby
sudo service postgresql status
```

You should get something like this

```apache
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
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672514497774/5ac81563-e17b-4f51-9893-25857160a320.png align="center")

---

### 4- Restart PostgresQL service

The following command is going to start the PostgresQL service:

```apache
sudo service postgresql start
```

The following command is going to restart the PostgresQL service.

```apache
sudo service postgresql restart
```

---

### 5- Enter the the PostgresQL terminal

```ruby
sudo -u postgres psql
```

You should be able to see something like this:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672514635050/31723443-0d3d-4296-b3c3-10acd7e9a4c8.png align="center")

---

### 6 - Check the roles/users

In order to check the current/available roles:

```apache
\du
```

You should be able to see something like the following:

```apache
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=#
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690990992239/e9308329-b824-4b15-96d7-b4dcb122b430.png align="center")

In my example, my role name is `calaca`.

---

### 7- Change the password

```apache
ALTER USER calaca WITH PASSWORD 'yourpassword';
```

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690991776199/458d4680-217b-4555-b13b-65b43c3b0928.png align="center")

---

### 8- Exit the PostgreSQL terminal

```apache
\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:

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

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

```apache
 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:

```apache
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691287015143/790331de-6266-47d1-9249-c0d63d8f1d00.png align="center")

---

## **Celebrate**

You've made it!

![The Office GIF - The Office Happy - Discover & Share GIFs](https://media.tenor.com/5F2BovYyscYAAAAd/the-office.gif align="left")

---

## **Let's become friends**

* [**Github**](https://github.com/alexcalaca)
    
* [**LinkedIn**](https://linkedin.com/in/alexandrecalacaofficial)
    
* [**Hashnode**](https://hashnode.com/onboard?next=/@alexandrecalaca)
    
* [**Youtube**](https://www.youtube.com/@alexandrecalacaofficial)
    

---

## **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.

---
