D Dean
D Dean

Reputation: 31

Unable to connect to Postgres from java application

I'm using Windows 10 Home edition, and used Docker Toolbox to get PostgreSQL installed.

I ran the following in Docker Toolbox to start up the database:

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.

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.

In the .yml file it's trying to connect to:

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?

Upvotes: 3

Views: 711

Answers (1)

Nicola Ben
Nicola Ben

Reputation: 11337

I run the postgres container with:

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

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.

For your problem I would check:

  1. Windows Firewall rules (port 5432 on localhost is reachable?)
  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 ;)

Upvotes: 1

Related Questions