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

Issue

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.

Resolution

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.

Ask on Stack Overflow

Engage with a community of passionate experts to get the answers you need

Ask on Stack Overflow

Heroku Support

Create a support ticket and our support experts will get back to you

Contact Heroku Support