简体   繁体   English

从Docker容器连接到Postgres

[英]Connecting to postgres from a docker container

I'm a little lost as to why my java application can't connect to my postgres database. 关于为什么我的Java应用程序无法连接到我的postgres数据库,我有些困惑。 I'm aiming to connect to a postgres database through jdbc. 我的目标是通过jdbc连接到Postgres数据库。 The application is to run inside a docker container. 该应用程序将在docker容器中运行。

this.connection = `DriverManager.getConnection("jdbc:postgresql://<myip>:5432/databasename", "usr", "password");`

I'm getting the exception: 我得到了例外:

Connection refused.  Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

When I run the application from my desktop, it connects as expected. 当我从桌面运行该应用程序时,它会按预期连接。 When I run it from within the docker container, it fails. 当我从docker容器中运行它时,它失败了。

I've just installed docker this afternoon and ran through the getting started for windows, so my setup state is just after running that. 我今天下午刚刚安装了docker,并完成了Windows入门,因此我的设置状态仅在运行完之后。 Here's the contents of my Dockerfile: 这是我的Dockerfile的内容:

FROM java:8
ADD VaultServer /
EXPOSE 3971
EXPOSE 3972
ENTRYPOINT ["java", "-jar", "VaultServer.jar"]

Inside the data folder there is a file called pg_hba.conf you have to configure it to accept the connections. 在数据文件夹中有一个名为pg_hba.conf的文件,您必须对其进行配置以接受连接。 So your pg_hba.conf file should have a line like this host all all YourDockerip/24 md5 . 因此,您的pg_hba.conf文件应具有这样一条线,即host all all YourDockerip/24 md5

After that configure the postgresql.conf file. 之后,配置postgresql.conf文件。 You have to update the listen_addresses to all and make sure to uncomment that line by removing the # mark. 您必须将listen_addresses更新为全部,并确保通过删除#标记取消注释该行。 So your listen_addresses should look like this listen_addresses = '*' . 因此,您的listen_addresses应该看起来像这个listen_addresses = '*'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM