Why can't my Java Spring app cannot connect to my database?


Your Spring application generates a java.net.ConnectException: Connection refused or similar error when attempting to make the first connection to your database, even though your JDBC URL is configured correctly in your application.properties or other configuration file.


Set your JDBC database URL as a system property such as -Dspring.datasource.url=jdbc:postgresql://... in your Procfile. This takes precedence over other forms of configuration including environment variables and configuration files.

The official Heroku buildpacks for Java and Gradle will attempt to create SPRING_DATASOURCE_URL, SPRING_DATASOURCE_USERNAME, and SPRING_DATASOURCE_PASSWORD environment variables when a dyno starts up. These variables will override configuration in your application.properties files, which can interfere with you app's ability to connect to your database.