I'm making a web app using spring boot but I don't wanna set connection url, username and password in application.properties
so I config DataSource programmatically via HikariCP.
Here is my code:
DBConfig.java:
@Configuration
public class DBConfig {
@Bean
public DataSource getDataSource() {
DataSource ds = null;
if (ds == null) {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(System.getenv("WJ_URL"));
config.setUsername(System.getenv("WJ_USER"));
config.setPassword(System.getenv("WJ_PASS"));
config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
ds = new HikariDataSource(config);
}
return ds;
}
}
I pretty sure that I set correct URL and another credentials info but it still got exception:
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.
Here is the connection url via system env:
WJ_URL=jdbc:postgresql://db.sakadream.me:5432/weebjournal