Install PostgreSQL in a docker container

I followed several tutorials as well as official ones, but when I try to install PostgreSQL in a container, I get the following message

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 looked through a few questions here on SO and around the internet, but no luck.

+13
source share
4 answers

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

To solve this problem, you must either explicitly request a tcp / ip connection when using the -p flag to configure the port for the postgres container, or share a Unix socket with HOST processing using the -v flag.

: Note: Using the -v or --volume= flag means that you are sharing some space between the HOST machine and the docker container. This means that if you have postgres installed on the host machine and it starts, you are likely to run into problems.

Below I demonstrate how to start the postgres container, which is accessible from tcp / ip and unix socket. I also call 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 think this is the most suitable. Finally, if the application / project that needs access is also a container, it’s better to simply link them.

+10
source share

By default, psql tries to connect to the server through a UNIX socket. That's why we see /var/run/postgresql/.s.PGSQL.5432- location of the UNIX socket descriptor.

If you run postgresql-server in a docker with a port binding, you must tell psql use a TCP socket. Just add a parameter:

psql -h localhost [any other params]

+3
source share

Here are instructions for fixing this error, which should also work for your docker container: PostgreSQL error 'Could not connect to the server: there is no such file or directory

If this does not work for any reason, there are many ready-made docker-containers-postgresql, for which you can look at the link to the Docker index: https://index.docker.io/search?q=postgresql

Many of the containers are built from trusted github repositories. Therefore, if you find one that seems to fit your needs, you can browse the source.

The postgresql device is also added to the Flynn project, which is worth checking out: https://github.com/flynn/flynn-postgres

+2
source share

FROM postgres: 9.6

RUN apt-get update && apt-get install -q -y postgresql-9.6 postgresql -c ontrib-9.6 postgresql -c lient -c ommon postgresql -c ommon 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

0
source share

All Articles