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 = #
Mike Sherrill 'Cat Recall'
source share