When I run the rake db:migrate
or run the rails s
command, I get the same error:
Error : could not connect to server:
No such file or
The convention for PostgreSQL packaged for Debian or Debian derivatives such as Ubuntu is to use /var/run/postgresql
as the directory for Unix domain sockets. On the other hand the convention for self-compiled postgres client libs is to use /tmp
, unless self-configured otherwise.
So the usual root cause of this mismatch between both is a mix of self-compiled client-side stuff with pre-compiled server-side packages (even if client and server are installed on the same machine, client-side and server-side are still distinct and can be out of sync).
Soft-linking from /tmp
to this directory as suggested by the asker works except that the link will be lost at every reboot, because in general /tmp
is emptied on reboot.
A better option would be to add as an entry in database.yml
:
either host: /tmp
if the real socket path is /tmp
(self-compiled server, packaged client)
or host: /var/run/postgresql
if the real socket path /var/run/postgresql/
(packaged server, self-compiled client).
When the value in the host field starts with a slash character, the postgres library knows that it's the location of a directory for local sockets rather than a hostname. The filename inside the directory .s.PGSQL.portnumber
is generated and must not be specified, only the directory.
Another possibility is to configure the self-compiled software packages as closely as possible to Debian, overriding the defaults as they do.