Installing PostgreSQL within a docker container

2019-02-16 10:03发布

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.

4条回答
放荡不羁爱自由
2楼-- · 2019-02-16 10:32

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

查看更多
别忘想泡老子
3楼-- · 2019-02-16 10:33

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 say psql to use TCP-socket. Just add param:

psql -h localhost [any other params]

查看更多
来,给爷笑一个
4楼-- · 2019-02-16 10:39

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

查看更多
forever°为你锁心
5楼-- · 2019-02-16 10:54

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.

查看更多
登录 后发表回答