It can connect to postgres through localhost without a password, but not through 127.0.0.1. What for?

I have a Kubuntu 14.10 desktop with a PostgreSQL 9.4 database installed. I changed the password of the postgres user in the database by executing SQL:

 ALTER USER postgres PASSWORD 'password'; 

And I can connect to the database server on psql -h localhost -U postgres -W and provide this password, but I can also connect without requiring a password just psql -h localhost -U postgres .

On the other hand, if I run psql -h 127.0.0.1 -U postgres , it will offer me the password set earlier.

What is the difference between localhost and 127.0.0.1 hosts and their login method? Where is it installed? In the pg_hba.conf I do not see localhost related links.

+7
postgresql ubuntu
source share
1 answer

The behavior you see may be caused by a password file. The password file is usually called ~ / .pgpass on Unix systems, but a different file name can be specified through the PGPASSFILE environment variable.

I think that a password file containing a string for "localhost" but not containing a string for "127.0.0.1" will show the behavior you see. My own ~ / .pgpass file contains this line.

  localhost: *: *: postgres: password

This is what happens when I try to connect just like you.

  $ psql -h localhost -U postgres
 psql (9.3.5)
 SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
 Type "help" for help.

 sandbox = # \ q
 $ psql -h 127.0.0.1 -U postgres
 Password for user postgres: 

Adding the line 127.0.0.1:*:*:postgres:password to ~ / .pgpass allows me to log in using 127.0.0.1 and the password.

  $ psql -h 127.0.0.1 -U postgres
 psql (9.3.5)
 SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
 Type "help" for help.

 sandbox = # 
+9
source share

All Articles