By default, in Ubuntu, the DB server socket should obviously be stored in the / var / run / postgresql folder, but every time I install it, it turns out to be in / tmp. Therefore, when most applications (eg Rails) access the server, this error appears:

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

How can I move a socket to the desired folder, or reassign system variables to search for it in another folder?

  • What does "initialize a string" mean? - Vitaly Liber

4 answers 4

Problem solved: uncomment and initialize the unix_socket_directory = '/var/run/postgresql/' line in the $PGDATA/postgresql.conf file and restart the server.

  • one
    and where is the $ PGDATA / postgresql.conf file? - Daniel Sayfulin

postgresql.conf is located in the /etc/postgresql/9.5/main directory

9.5 - the version of the database, you may differ.

    Kubuntu 18.04 I was helped by copying files from /etc/postgresql/10/main to /var/lib/postgresql/10/main This can only be done by giving permissions to the chmod 777 main entry and then returning to chmod 700 , or by using the postgres user . Be sure to look at the log file which in my case lies in /var/lib/postgresql .

      For the PostgreSQL source code, the default is /tmp .

      However, this path is not nailed, it is a constant in the DEFAULT_PGSOCKET_DIR code and some accompanying packages prefer to change the path to the preferred one for the distributor. In particular, packages for Ubuntu and Debian are usually built with this path changed to /var/run/postgresql/ .

      And this change does not pose any problems, but only as long as your applications using the database are compiled with the same DEFAULT_PGSOCKET_DIR . If the libpq library used on the system was compiled with one DEFAULT_PGSOCKET_DIR , and a DBMS with another, then you will receive such errors. Therefore, it is best to check which particular packages you use for the DBMS itself, and which applications and libpq . Obviously they are compiled with different settings, possibly derived from different repositories. As a postgresql DBA, I recommend using the PostgreSQL Global Development Group repositories.


      Another possibility, as already noted, is to configure the DBMS to listen to the socket not at the address with which it was compiled, but with the specified one manually. The configuration option unix_socket_directories is responsible for unix_socket_directories ( here you can see that the default value is the constant DEFAULT_PGSOCKET_DIR )

      Where is the postgresql.conf configuration file itself - the question is separate and specific again for the preferences of the person who collected the DBMS. Although this is not an assembly option (the name postgresql.conf hard-coded in the paths), it is located in a directory called $PGDATA . That is, anywhere, if only this directory was specified during initialization and then at each start of the DBMS. As well as the configuration file can be located anywhere if the launch option config_file to specify the path to it when you start the database.

      In ubuntu and debian usually laid out like this: - configuration files in /etc/postgresql/(номер major версии postgresql)/main - $PGDATA is located along the path /var/lib/postgresql/(номер major версии postgresql)/main

      In CentOS, for example, $PGDATA is located in /var/lib/pgsql/(номер major версии postgresql)/data , the same configuration file.

      But if, as in your case, there is an inconsistent state of installed packages, then there may be surprises. It's guaranteed to find out from which configuration file PostgreSQL started working by request.

       show config_file; 

      In response, there will be a path to the configuration file. It remains only to bring the unix_socket_directories option to the unix_socket_directories = '/var/run/postgresql/' and restart the DBMS: unix_socket_directories can be changed only when restarting the entire DBMS.

      PS: it is worth commenting that before release 9.3 the option was called unix_socket_directory . Modern unix_socket_directories allows listing of several directories separated by commas.