/etc/hosts file being ignored
I found the problem.
The /etc/hosts file had overly restrictive permissions -rw------ (I believe that is 600 octal). Changing that to mode 644 fixed the problem.
My tests with ping were done with ordinary priv, and the hosts file was unreadable. My original problem was getting postgresql to startup properly; it starts with root privilege, but apparently sheds priv before starting its child processes for autovacuum and such, so by the time it needed a domain resolved, it could not read the hosts file.
Thanks to everybody who replied. It has been enlightening.
Check /etc/nsswitch.conf
You should have a line like:
hosts: files dns
if the "files" part is missing, /etc/hosts won't be checked.
the hosts file is strange sometimes on some distrib, try put all localhost alias in the same line
like this :
127.0.0.1 localhost.localdomain localhost mydomain.com nowhere