Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing PostgreSQL within a docker container

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.

like image 380
Hevlastka Avatar asked May 18 '14 17:05

Hevlastka


People also ask

Where does a postgres store data in a Docker container?

To circumvent this issue, we can use the information we gathered earlier that showed us that the volume is mounted at /var/lib/postgresql/data. Inside the container, this directory is where Postgres stores all the relevant tables and databases.

What is postgres Docker?

PostgreSQL is a free, open-source and object-relational database management system. It is used by developers because it is stable, powerful and flexible. Docker is an open-source tool that makes it easier to create, deploy, and run applications by using containers.


1 Answers

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.

like image 91
Vasspilka Avatar answered Sep 22 '22 22:09

Vasspilka