How to make psql to default to localhost?

Solution 1:

If you don't like the default of connecting via a unix socket, you can set the environment variable PGHOST. I have

export PGHOST="db"

in my ~/.bashrc, to connect to the server with that name. A few more handy environment variables are documented at http://www.postgresql.org/docs/9.5/static/libpq-envars.html

Solution 2:

In fact, it works as documented.

When the host name is omitted, psql connects to the Unix-domain socket whose path is compiled in. That part works in your case, otherwise it would output a different error message than what the question shows.

The error message in the question:

psql: FATAL:  Ident authentication failed for user "postgres"

relates to the fact that only the Unix user postgres has the permission to connect as the database user postgres when using a Unix-domain socket. This is configured in the pg_hba.conf file.

what you should typically do in Ubuntu when you're logged in as a normal user, but you're the administrator of the system:

$ sudo -u postgres psql