databasepostgresqlconfigurationcreateuser

createuser could not connect to database postgres


Please don't move this question to askubuntu as I think this question is not OS-specific.

When I invoke the createuser postgres command (for now it doesn't matter if I provide any parameters or not), I'm getting this error:

createuser: could not connect to database postgres: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Normally it means that the postgres server is down but not this time:

pg_lsclusters
Ver Cluster Port Status Owner    Data directory               Log file
9.4 main    5432 online postgres /var/lib/postgresql/9.4/main /var/log/postgresql/postgresql-9.4-main.log

sudo service postgresql status
9.4/main (port 5432): online

But it's true that there is no /tmp/.s.PGSQL.5432 file because my configuration file (/etc/postgresql/9.4/main/postgresql.conf) has this line:

unix_socket_directories = '/var/run/postgresql'

So I don't really understand why createuser whants to access /tmp/.s.PGSQL.5432? Can this path can be hardcoded into the createuser binary? I don't see any command line argument to specify the settings file location for createuser...


Solution

  • The postgresql.conf file is read by the database server, but not by client applications (such as createuser, psql, ...). (In fact, the server configuration file cannot be read by client applications because the client would have to connect to the server, which could be halfway across the world, before it could possibly know where that configuration file lives).

    Instead, you have to tell your client application where to find the socket directory.

    If your client application (createuser) is connecting to the local host (which is must be because you are not specifying a different host), you use the host parameter to specify the name of the socket directory.

    For example: createuser -h /var/run/postgresql newusername

    See http://www.postgresql.org/docs/devel/static/libpq-connect.html#LIBPQ-CONNECT-HOST

    Hope that helps.