Installing PostgreSQL within a docker container

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.


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.