I'm running a rails app in development with postgresql 9.3. When I tried to start passenger server today, I got:

PG::ConnectionBad - could not connect to server: Connection refused
    Is the server running on host "localhost" ( and accepting
    TCP/IP connections on port 5432?

No big deal I thought, that happened before. Restarting postgres always solved the problem. So I ran sudo service postgresql restart and got:

 * Restarting PostgreSQL 9.3 database server
 * The PostgreSQL server failed to start. Please check the log output:
2014-06-11 10:32:41 CEST LOG:  could not bind IPv4 socket: Cannot assign requested address
2014-06-11 10:32:41 CEST HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2014-06-11 10:32:41 CEST WARNING:  could not create listen socket for "localhost"
2014-06-11 10:32:41 CEST FATAL:  could not create any TCP/IP sockets

My postgresql.conf points to the defaults: localhost and port 5432. I tried changing the port but the error message is the same (except the port change).

Both ps aux | grep postgresql and ps aux | grep postmaster return nothing.


In postgresql.conf I changed listen_addresses to instead of localhost and it did the trick, server restarted. I also had to edit my applications' db config and point to instead of localhost. However, the question is now, why is localhost considered to be and not

That's my /etc/hosts:   local   jacek-X501A1   something.name.non.example.com   company.something.name.non.example.com
  • any output from sudo netstat -anlp | grep 5432? – Flup Jun 11 '14 at 8:45
  • 1
    If you're going to obfuscate your data, please do so responsibly and use example.com as the domain name. – Jenny D says Reinstate Monica Jun 11 '14 at 9:06

Your /etc/hosts is broken. The first line should read   localhost something.name.non.example.com company.something.name.non.example.com
  • Note for future readers that even if you do have a localhost entry in /etc/hosts, various other factors can cause a different DNS response when a DNS lookup of localhost is actually run. See stackoverflow.com/a/47824848/5419599. – Wildcard Dec 15 '17 at 2:11
  • @Wildcard I do not know which OS that answer would be applicable to (if any). It certainly doesn't make much sense on Linux. – kasperd Jan 4 at 16:19
  • 1
    @kasperd I suppose if you've got the order dns files instead of files dns in nsswitch.conf, and have a name server that resolves localhost, you could get in trouble. But that does sound highly unlikely to me... – Jenny D says Reinstate Monica Jan 5 at 12:11
  • @JennyD The answer linked above suggests using nslookup to verify. But the nslookup command on Linux doesn't use nsswitch.conf and /etc/hosts in the first place. And as for putting dns files in the configuration, that sounds like asking for trouble to me. – kasperd Jan 5 at 12:58
  • 1
    @kasperd Agreed - it would be fairly high on the "it's-your-own-damned-fault" scale... – Jenny D says Reinstate Monica Jan 7 at 7:27

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.