How to Change User Password in Postgres

Shihab Sikder Feb 02, 2024
  1. Change the User Password in Postgres Using Windows
  2. Change the User Password in Postgres Using Linux
How to Change User Password in Postgres

In this tutorial, we will change the user password in Postgres.

Change the User Password in Postgres Using Windows

  • Open the SQL Shell (psql) from the menus or search bar.
  • Connect to the default database with the default port. If you set up with default settings, then the configuration should look like this:
    Server [localhost]:
    Database [postgres]:
    Port [5432]:
    Username [postgres]:
    Password for user postgres:

If you put the field blank and press empty, it will take the default values shown between the square bracket.

Then, log in with the default password you set during the installation. If you’re successful, then you will get the SQL command line:

psql (14.0)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

postgres=#

Now, if you want to see all the user lists, type the command:

postgres=# select * from USER;
   user
----------
 postgres
(1 row)

You need to alter the user information using ALTER to change the user password. The query format for changing the user password:

postgres=# ALTER USER postgres WITH PASSWORD 'ROOT';
ALTER ROLE
postgres=#

Instead of postgres, you can type your username, which you want to update, and instead of Root, you can use your defined password.

Or, there’s another method for changing the passwords without writing any SQL query. After opening the psql command line, type \password user_name.

Then it will ask for a new password and again ask to retype the new password.

postgres=# \password postgres
Enter new password: <Enter-your-password>
Enter it again: <Retype-your-password>
postgres=#

Change the User Password in Postgres Using Linux

You can enter the psql console without any password. Use the sudo command.

Here’s how you can log in without a psql password:

$sudo -u <username> psql <database>

Use the above method to change the password for a user in psql.

It’s a good practice to save the user password encrypted. You can use the following command:

postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'ROOT';
Shihab Sikder avatar Shihab Sikder avatar

I'm Shihab Sikder, a professional Backend Developer with experience in problem-solving and content writing. Building secure, scalable, and reliable backend architecture is my motive. I'm working with two companies as a part-time backend engineer.

LinkedIn Website