简体   繁体   English

无法从Java应用程序连接到Postgres

[英]Unable to connect to Postgres from java application

I'm using Windows 10 Home edition, and used Docker Toolbox to get PostgreSQL installed. 我正在使用Windows 10 Home Edition,并使用Docker Toolbox来安装PostgreSQL。

I ran the following in Docker Toolbox to start up the database: 我在Docker Toolbox中运行以下命令来启动数据库:

docker ​​run​​ --name ​​mydb_postgres​​ -p ​​5432:5432 ​​-e POSTGRES_PASSWORD=mydb​ ​-e​POSTGRES_USER=mydb -e POSTGRES_DB=mydb postgres

After running this, it seems like something shuts it down, but as you can see from the last line it looks like it was successful. 在运行之后,似乎有些事情将其关闭,但正如您从最后一行所看到的那样,它看起来很成功。

Also when I run docker ps -a I can see that the container is up and running. 此外,当我运行docker ps -a我可以看到容器已启动并正在运行。

Logs 日志

2018-06-19 20:53:20.430 UTC [38] LOG:  received fast shutdown request
waiting for server to shut down....2018-06-19 20:53:20.461 UTC [38] LOG:  
aborting any active transactions
2018-06-19 20:53:20.467 UTC [38] LOG:  worker process: logical replication launcher (PID 45) exited with exit code 1
2018-06-19 20:53:20.468 UTC [40] LOG:  shutting down
2018-06-19 20:53:20.913 UTC [38] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2018-06-19 20:53:21.013 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2018-06-19 20:53:21.014 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2018-06-19 20:53:21.095 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2018-06-19 20:53:21.266 UTC [65] LOG:  database system was shut down at 2018-06-19 20:53:20 UTC
2018-06-19 20:53:21.301 UTC [1] LOG:  database system is ready to accept connections

Now I'm trying to boot up a server which connects to this database when it boots up using DropWizard and a config .yml file. 现在我正在尝试使用DropWizard和config .yml文件启动连接到此数据库的服务器。

In the .yml file it's trying to connect to: .yml文件中,它正在尝试连接到:

url: "jdbc:postgresql://localhost:5432/mydb"

Error thrown in the server when it boots up: 启动时在服务器中抛出错误:

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

Some people have mentioned to change the listening ports to "*" in PostgreSQL but I'm not sure if that's right - if it is - what's the best way to do this using docker? 有些人提到在PostgreSQL中将监听端口更改为“*”,但我不确定这是否正确 - 如果是 - 使用docker执行此操作的最佳方法是什么?

I run the postgres container with: 我运行postgres容器:

docker run --name mydb_postgres -p 5432:5432 -e POSTGRES_PASSWORD=mydb -ePOSTGRES_USER=mydb -e POSTGRES_DB=mydb --rm postgres

and then checked the listening port with: ss -ltpn 然后使用以下命令检查侦听端口: ss -ltpn

State      Recv-Q Send-Q Local Address:Port  Peer Address:Port               
LISTEN     0      128    :::5432             :::*

I succesfully can connect to the database: 我成功连接到数据库:

# psql -h localhost -p 5432 -Umydb -W  
Password for user mydb: 

psql (9.6.7, server 10.4 (Debian 10.4-2.pgdg90+1))  WARNING: psql major version 9.6, server major version 10.
       Some psql features might not work. Type "help" for help.

mydb=# 

I have done my test on linux. 我已经在linux上完成了测试。

For your problem I would check: 对于你的问题,我会检查:

  1. Windows Firewall rules (port 5432 on localhost is reachable?) Windows防火墙规则(localhost上的端口5432是否可访问?)
  2. Your connection to the database is done from the same container or from annother one? 您与数据库的连接是从同一个容器还是另一个容器完成的? In the second case you need to link them 在第二种情况下,您需要链接它们
  3. yml file has written the correct credentials (I am sure it has but I needed to write three points in my list ;) yml文件写了正确的凭据(我确信它有,但我需要在我的列表中写三个点;)

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

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