The post will explain, how to set postgres password on PostgreSQL 9.4 . We expect , you have already install postgres 9.4 on your Ubuntu system .

The user called postgres has all privileges to work in postgres server. It is like an administrator. After installing postgreSQL 9.4 , by default without password user can login into postgres server console. This is very important to set password on Postgres server.
Given below is example of what we are talking in above section –
psql

Set postgres password

Setting postgres password is quite tricky , it is unlike MySQL . You have to go with some editing with file.

Let’s start setting postgres password.

1. Set password for postgres

We are quickly writing to set postgres password.

# Switch to postgres user
$ sudo su - postgres

# Now Login into postgres server
$ psql

## Running ' password postgres ' to set password
password postgres

# To quite from postgres
q

Given below is reference from our system. This will help you to understand more easily.

sharad@ubuntu:~$ sudo su - postgres
[sudo] password for sharad: 
postgres@ubuntu:~$ 
postgres@ubuntu:~$ psql
psql (9.4.4)
Type "help" for help.

postgres=# 
postgres=# password postgres
Enter new password: 
Enter it again: 
postgres=# 
postgres=# q
postgres@ubuntu:~$ 

Now logout from postgres system user
Run below command to exit.

exit

2. Edit /etc/postgresql/9.4/main/pg_hba.conf

Now edit the file called pg_hba.conf to make it effective.

## Taking backup of original file
sudo cp -pv /etc/postgresql/9.4/main/pg_hba.conf /etc/postgresql/9.4/main/pg_hba.conf.orig

## now editing
sudo vi /etc/postgresql/9.4/main/pg_hba.conf

Do the editing as described in screenshot –

pg_hba.conf

The given below is reference from our system, we have only 3 lines in pg_hba.conf file.

root@ubuntu:/etc/postgresql/9.4/main# egrep -v '^#|^$' pg_hba.conf
local   all             all                                     md5
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
root@ubuntu:/etc/postgresql/9.4/main#

Now restart the postgres service

sudo service postgresql restart

3. Test login

Now test by login into postgres server. Given below reference is from our system.

sharad@ubuntu:~$ sudo su - postgres
[sudo] password for sharad: 
postgres@ubuntu:~$ 
postgres@ubuntu:~$ 
postgres@ubuntu:~$ psql
Password: 
psql (9.4.4)
Type "help" for help.

postgres=# 
postgres=# q
postgres@ubuntu:~$ 
postgres@ubuntu:~$

If you have set password for other postgres user, then to login into system use following syntax.

psql -U user-name -W

See the given below example.

postgres@ubuntu:~$ psql -U postgres -W
Password for user postgres: 
psql (9.4.4)
Type "help" for help.

postgres=# 
postgres=#

Read Some More Articles

5 Comments

  1. Hi Sharad,

    Great instruction set, your screenshot image for the pg_hba.conf modifications is no longer displaying.

    Any chance you could update or provide that info as text please?

    Thanks again!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.