I have noticed that although the listening host in postgresql.conf is localhost connecting to a database via psql fails unless the host is changed I have also noticed MySQL display the same trait on occasion.

Is there some difference between and localhost which causes the database connections to be treated differently?

  • 1
    Are you running more than one database your system? Do you have more than one instance of the database running? – user12964 Jun 24 '15 at 16:17
  • 1
    Generally it's because localhost resolves to an IPv6 address such as ::1. In any case, when psql fails to connect, it displays an error message which should be included in your question. – Daniel Vérité Jun 24 '15 at 17:51
  • This question is lacking basic information. Your OS? Your Postgres version? Which setting in postgresql.conf exactly? Connecting with psql (same version?) from where exactly? How exactly? Fails how exactly? Please be more clear. – Erwin Brandstetter Jun 25 '15 at 1:21

this has nothing to do with databases. its how you os resolves the domain name in this case 'localhost'

you /etc/hosts file should have an entry for localhost as below if you don't have it , then adding this entry will resolve your issue localhost localhost.localdomain
  • I have got localhost in my /etc/hosts file, but not localhost.localdomain, and my pg_hba.conf files contains but not localhost. Could these be factors? – vfclists Jun 24 '15 at 22:30
  • yes, since you are specifying actual IP it expects you to use the same. In case if you use a domain name for e.g.: localhost , only then it will try to resolve. and in that case also, it will expect you to use localhost. it is always good to use ip since , someone could edit the hosts file as below and 192.168.x.x localhost localhost.localdomain.com so now you localhost is the local any more – bhavinpatel Jun 25 '15 at 3:18

If you are connecting via psql -h or psql -h localhost or just psql, it will determine HOW psql connects (either via TCP/IP or to a local socket, respectively).

postgres' pg_hba.conf file may have an entry allowing to connect to TCP/IP (a line starting with host), but not to a socket (a line starting with local).

Review your records in that file and verify that localhost is allowed to connect. It is likely located in the same directory that your postgres.conf file is in.

You can find more details in this question

I should add that I have encountered this behavior previously and it turned out that I had removed the local line from my pg_hba.conf file. When I added it back in, it worked fine.


Check your HOSTS file to see if you didn't screw it up (duplicate lines, wrong lines, etc). It should only have this in it: localhost

  • ... at least the only line related to the issue. It's not uncommon to have a bunch of entries there, like in my case, for some VMs and other stuff. – dezso Jun 24 '15 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.