How to Set the Default User Password in PostgreSQL

Maintaining PostgreSQL's security is essential for database administrators. One way to accomplish this task is by setting a strong and unique password for the default user. This will protect your data and prevent unauthorized access to your database. In PostgreSQL, “postgres” is the default user, so you must log in as “postgres” to set the default user password.

This post will present a detailed guide on how to set/change the password for the default user in PostgreSQL. So, let’s start!

How Do I Change/Set the Default User Password in Postgres?

PostgreSQL offers a couple of built-in commands, such as “ALTER USER” and “ALTER ROLE”, that are used to modify the user’s password. To set the default user password in Postgres, you can follow these steps:

Step 1: Login as Default User

Launch the SQL Shell, and provide the current password, as shown in the following snippet:

img

Hit the enter button to log in as the default user:

img

The above snippet shows that you have successfully logged in as the default user.

Step 2: Change/Set the Default User Password

Now, execute the “ALTER ROLE” or “ALTER USER” statement to modify the default user password:

ALTER USER postgres PASSWORD 'new_password';
img

The above snippet shows that the default use has been altered successfully.

Alternatively, you can execute the “ALTER ROLE” statement as follows:

ALTER ROLE postgres PASSWORD 'my_new_password';
img

The “ALTER ROLE” message in the output indicates that the default user password has been set/changed successfully.

This is how you can set the default user password in Postgres using “ALTER ROLE” or “ALTER USER” statements.

Conclusion

PostgreSQL provides a couple of built-in statements, such as “ALTER USER” and “ALTER ROLE” that are used to modify the user’s password. In PostgreSQL, “postgres” is the default user, so you must log in as “postgres” to set the default user password. After that, execute either the “ALTER USER” or “ALTER ROLE” command with the PASSWORD attribute to set the default user password. This blog post presented step-by-step instructions to set the default user password in Postgres.