Change postgres Password via Command Line
This is an article containing on how to reset or to change password of a default account exist in PostgreSQL Database Server. The account itself has a default username called ‘postgres’. To be able to reset or to initialize even to change it further. It is very simple and easy where the following are steps to achieve that :
1. Access PostgreSQL Command Console by typing the following command in any bash prompt available :
psql -Upostgres
user@hostname:~$ psql Password: psql: FATAL: password authentication failed for user "user" user@hostname:~$
The above is an output generated by PostgreSQL Commad Console where the command is not completely executed because the command didn’t have any user account supplied. So, based on the command pattern given above, type it as shown in the following output :
user@hostname:~$ psql -Upostgres Password for user postgres: psql: FATAL: password authentication failed for user "postgres" user@hostname:~$
The above output displayed whenever the supplied password for the user given is incorrect. In this article, the process for changing the password is only working whenever the password has never been initiated or the old password is already known. B
user@hostname:~$ psql -Upostgres Password for user postgres: psql (9.5.3, server 9.3.10) Type "help" for help. postgres=#
The output generated in the above is presented whenever the password entered for account ‘postgres’ is correctly inserted. That means, the next process for changing the old password for user ‘postgres’ can easily be done from PostgreSQL Command Console.
- Type the following command in PostgreSQL Command Console :
alter user postgres with encrypted password 'your_new_password'
Try to execute the above command in a PostgreSQL Command Console :
postgres=# alter user postgres with encrypted password 'mynewpassword'; ALTER ROLE postgres=#
- After successfully alter or change the password, quit from PostgreSQL Command Console by typing ‘\q’ to quit as shown below :
postgres=# \q user@hostname:~$
- Type the following to try to login using the new password :
user@hostname:~$ psql -Upostgres Password for user postgres: psql (9.5.3, server 9.3.10) Type "help" for help. postgres=#
- After successfully connected to PostgreSQL Command Console, basically the process for changing ‘postgres’ default account’s password has already finished.