39

I've been following several different tutorials as well as the official one however whenever I try to install PostgreSQL within a container I get the following message afterwards

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

I've looked through several questions here on SO and throughout the internet but no luck.

Hevlastka
  • 1,878
  • 2
  • 18
  • 29

5 Answers5

32

The problem is that the your application/project is trying to access the postgres socket file in the HOST machine (not docker container).

To solve it one would either have to explicitly ask for an tcp/ip connection while using the -p flag to set up a port for the postgres container, or share the unix socket with the HOST maching using the -v flag.

:NOTE: Using the -v or --volume= flag means you are sharing some space between the HOST machine and the docker container. That means that if you have postgres installed on your host machine and its running you will probably run into issues.

Below I demonstrate how to run a postgres container that is both accessible from tcp/ip and unix socket. Also I am naming the container as postgres.

docker run -p 5432:5432 -v /var/run/postgresql:/var/run/postgresql -d --name postgres postgres

There are other solutions, but I find this one the most suitable. Finally if the application/project that needs access is also a container, it is better to just link them.

Vasspilka
  • 1,135
  • 1
  • 10
  • 22
  • 1
    Why does the volume matter here, and how can I run a similar command but persist the data? I assume I would name the volume? – Vincent Buscarello Jun 20 '18 at 04:18
  • 1
    @VincentBuscarello you would create a volume for where the data is stored that means linking `/var/lib/postgresql/data` from inside the container to somewhere outside – Vasspilka Jun 27 '18 at 09:51
  • > inking /var/lib/postgresql/data from inside the container to somewhere outside Is that what the `:` does? how is that done? – Vincent Buscarello Jun 27 '18 at 16:21
  • 5
    @Vasspilka but what should i do if i want to run my image from docker-compose.yml – Humayun Naseer Jan 06 '21 at 10:22
  • This won't work on Mac, as it is not possible to share Unix Sockets from a docker container (Linux) with MacOS as the host: https://github.com/docker/for-mac/issues/483 – Marnix.hoh Jun 29 '23 at 14:27
19

By default psql is trying to connect to server using UNIX socket. That's why we see /var/run/postgresql/.s.PGSQL.5432- a location of UNIX-socket descriptor.

If you run postgresql-server in docker with port binding so you have to tell psql to use TCP-socket. Just add host param (--host or -h):

psql -h localhost [any other params]

UPD. Or share UNIX socket descriptor with host (where psql will be started) as was shown in main answer. But I prefer to use TCP socket as easy managed approach.

Timur Milovanov
  • 727
  • 8
  • 18
0
FROM postgres:9.6

RUN apt-get update && apt-get install -q -y postgresql-9.6 postgresql-client-9.6 postgresql-contrib-9.6 postgresql-client-common postgresql-common
RUN echo postgres:postgres | chpasswd

RUN pg_createcluster 9.6 main --start

RUN /etc/init.d/postgresql start

RUN su -c "psql -c \"ALTER USER postgres PASSWORD 'postgres';\"" postgres
tgogos
  • 23,218
  • 20
  • 96
  • 128
-1

Here are instructions for fixing that error that should also work for your docker container: PostgreSQL error 'Could not connect to server: No such file or directory'

If that doesn't work for any reason, there are many of off-the-shelf postgresql docker containers you can look at for reference on the Docker Index: https://index.docker.io/search?q=postgresql

Many of the containers are built from trusted repos on github. So if you find one that seems like it meets your needs, you can review the source.

The Flynn project has also included a postgresql appliance that might be worth checking out: https://github.com/flynn/flynn-postgres

Community
  • 1
  • 1
  • 1
    Thanks @toddsampson but it seems like the error you linked is not the issue, nevertheless I've decided to use a preconfigured image from the index you recommended. I realise that I haven't provided much information but could you perhaps suspect what might be causing such behaviour? – Hevlastka May 19 '14 at 19:55
  • 1
    @Hevlastka I'm curious: if this answer didn't provide you with a solution, why did you accept it? I'm hitting the same issue right now, and it's a bit frustrating to see an accepted answer which doesn't solve the problem. – Mihai Dec 13 '15 at 13:52
  • 1
    @Mihai I've accepted this as an answer because it placed me on the right track to find the solution. Turned out that the error I've experienced was actually a bogus one hiding the real error behind it. Take a look at the [official postgres Dockerfile](https://github.com/docker-library/postgres/blob/ed23320582f4ec5b0e5e35c99d98966dacbc6ed8/9.4/Dockerfile). In my case the postgres server never started up therefore I had to create a custom script that was called using ENTRYPOINT. – Hevlastka Dec 17 '15 at 12:24
  • @Hevlastka can you please share that script or at least the basic part? – chachan Jul 05 '16 at 10:25
  • @chachan the bash I'm currently using can be found [here](https://github.com/docker-library/postgres/blob/master/9.4/docker-entrypoint.sh). I'll link my old init bash file if I can find it! – Hevlastka Jul 05 '16 at 10:34
-1

Run the below command to create a new container with PSQL running it it, which can be accessed from other containers/applications.

docker run --name postgresql-container -p 5432:5432 -e POSTGRES_PASSWORD=somePassword -d postgres

Now, export the connection-string or DB credentials from ur .env and use it in the application. Refernce: detailed installion and running

ShreeJ
  • 294
  • 4
  • 3