在Docker容器中安装PostgreSQL [英] Installing PostgreSQL within a docker container

查看:766
本文介绍了在Docker容器中安装PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  psql:无法连接到服务器:没有这样的文件或目录
服务器是否在本地运行并接受Unix域套接字 /var/run/postgresql/.s上的
连接.PGSQL.5432?

我在SO和整个互联网上浏览了几个问题,但是没有运气。

解决方案

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

要解决该问题,必须使用 -p 标志显式地请求tcp / ip连接设置postgres容器的端口,或使用 -v 标志与HOST共享共享unix套接字。



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



下面,我演示如何运行一个都可访问的postgres容器从TCP / IP和Unix套接字。我也将容器命名为 postgres



docker run -p 5432:5432 -v / var / run / postgresql:/ var / run / postgresql -d --name postgres postgres



有其他解决方案,但我发现这是最合适的。最后,如果需要访问的应用程序/项目也是容器,则最好将它们链接起来。


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.

解决方案

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.

这篇关于在Docker容器中安装PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆