在docker容器中安装PostgreSQL

时间:2014-05-18 17:17:35

标签: docker postgresql-9.3

我一直在关注几个不同的教程以及官方的教程,但每当我尝试在容器中安装PostgreSQL时,我会收到以下消息

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

我已经在SO和整个互联网上查看了几个问题,但没有运气。

5 个答案:

答案 0 :(得分:10)

问题是您的应用程序/项目正在尝试访问HOST计算机中的postgres套接字文件(而非docker容器)。

要解决这个问题,要么在使用-p标志为postgres容器设置端口时要么显式请求tcp / ip连接,要么使用{与HOST maching共享unix socket。 {1}}旗帜。

:注意: 使用-v-v标志意味着您在HOST计算机和docker容器之间共享一些空间。这意味着如果你在主机上安装了postgres并且它正在运行,你可能会遇到问题。

下面我将演示如何运行可从tcp / ip和unix socket访问的postgres容器。我也将容器命名为--volume=

postgres

还有其他解决方案,但我觉得这个最合适。最后,如果需要访问的应用程序/项目也是一个容器,最好只链接它们。

答案 1 :(得分:2)

以下是修复该错误的说明,该错误也适用于您的泊坞广告容器:PostgreSQL error 'Could not connect to server: No such file or directory'

如果由于某种原因无法正常工作,那么您可以查看许多现成的postgresql docker容器以供Docker索引参考:https://index.docker.io/search?q=postgresql

许多容器都是在github上使用可靠的repos构建的。因此,如果您发现它看起来符合您的需求,您可以查看来源。

Flynn项目还包括一个可能值得一试的postgresql设备:https://github.com/flynn/flynn-postgres

答案 2 :(得分:2)

默认情况下,psql尝试使用UNIX套接字连接到服务器。这就是为什么我们看到 /var/run/postgresql/.s.PGSQL.5432 -UNIX套接字描述符的位置的原因。

如果您在具有端口绑定的docker中运行 postgresql-server ,则必须说psql才能使用TCP-socket。只需添加参数:

psql -h localhost [any other params]

答案 3 :(得分:0)

从Postgres:9.6

运行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

运行pg_createcluster 9.6 main --start

运行/etc/init.d/postgresql start

运行su -c“ psql -c \” ALTER USER postgres PASSWORD'postgres'; \“” postgres

答案 4 :(得分:0)

运行以下命令,用运行它的PSQL创建一个新容器,可以从其他容器/应用程序访问它。

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

现在,从ur .env导出连接字符串或数据库凭据,并在应用程序中使用它。 推荐人:detailed installion and running