0

How to fix this error?

psql: error: 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"?

We use Docker, Postgres12, Wercker. When we run this command in Wercker, we can't create a database. But we can create it in my local Ubuntu environment.

sudo docker exec docker_postgres_1 psql -U postgres -c 'create database postgres_dev'
ThanhPhanLe
  • 1,315
  • 3
  • 14
  • 25
guitar-man
  • 13
  • 4
  • Did you try reading logs from postgres startup? – Łukasz Kamiński Aug 02 '19 at 11:00
  • It seems you are trying to connect to your database using socket (intended or not). In you postgresql.conf, make sure you have uncommented "unix_socket_directories" and that it is pointing to the directory specified in your error. Also make sure you have local (socket) access enabled in pg_hba.conf – Matúš Zábojník Aug 02 '19 at 11:47
  • If you want to connect using tcp just add host param to your param list "-h localhost" – Matúš Zábojník Aug 02 '19 at 14:41

1 Answers1

0

First, you need to make sure the socket file is located in /var/run/postgresql/.s.PGSQL.5432. To check that

$ cat /var/run/postgresql/.s.PGSQL.5432

if result shows something, then the problem is anything else. But, if file is not there you need to check /tmp dir (specially for OSX Homebrew users)

$ cd /tmp
$ l

total 16
drwxrwxrwt  7 root   wheel   224B Mar 11 08:03 .
drwxr-xr-x  6 root   wheel   192B Jan 23 18:35 ..
-rw-r--r--  1 root   wheel    65B Nov  7 22:59 .BBE72B41371180178E084EEAF106AED4F350939DB95D3516864A1CC62E7AE82F
srwxrwxrwx  1 shiva  wheel     0B Mar 11 08:03 .s.PGSQL.5432
-rw-------  1 shiva  wheel    57B Mar 11 08:03 .s.PGSQL.5432.lock
drwx------  3 shiva  wheel    96B Mar 10 17:11 com.apple.launchd.C1tUB2MvF8
drwxr-xr-x  2 root   wheel    64B Mar 10 17:10 powerlog

Now, there are two ways you can solve the error

Solution One

You can change the application configuration to see for sockets at /tmp/.s.PGSQL.5432

For Rails Users

# config/database.yml

default: &default
  adapter: postgresql
  pool: 5
  # port: 
  timeout: 5000
  encoding: utf8
  # min_messages: warning
  socket: /tmp/.s.PGSQL.5432

Solution Two

You can create symlinks to the expected location

$ sudo mkdir /var/pgsql_socket
$ sudo ln /tmp/.s.PGSQL.5432 /var/pgsql_socket/
```

Then the error should go. 

Hope this helps.
Shiva
  • 11,485
  • 2
  • 67
  • 84