'Cannot connect Spring Boot to remote PostgreSQL server

I'm trying to connect my Spring Boot application to a remote PostgreSQL server database.

My application.properties file has the following:

server.port=8102
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql:192.168.4.33:5432/postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=postgres
spring.datasource.password=secretpassword
spring.jpa.show-sql=true
logging.level.org.springframework.web=debug
logging.level.org.hibernate=debug
spring.jpa.generate-ddl=true

However, when I try to run my app, it comes out with the following error:

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

I'm not sure why Spring Boot is trying to use localhost still as I'm sure I've specifically asked it to use the IP address instead.



Solution 1:[1]

you have connection refuse.please make sure your database up and correct username and pass.or network is ok.

jdbc:postgresql://HOST:PORT/activitydb

Solution 2:[2]

Make sure the URL is correct.

spring.datasource.url=jdbc:postgresql:192.168.4.33:5432/postgres

You should use spring.datasource.url=jdbc:postgresql://192.168.4.33:5432/postgres

if it doesn't work then I suggest looking at your firewall or checking if that port is open on the server.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zahra souri
Solution 2