title: Getting a Connection to a Postgresql database from a Datasource created with Spring's DataSourceBuilder
body: <p>I am new to Spring and Spring Boot. I am trying to connect to a Postgresql database in my Spring Boot app.
  I've tried to take inspiration from</p>
  
  <p><a href="http://spring.io/guides/gs/accessing-data-mysql/" rel="nofollow noreferrer">http://spring.io/guides/gs/accessing-data-mysql/</a></p>
  
  <p>and</p>
  
  <p><a href="https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html" rel="nofollow noreferrer">https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html</a></p>
  
  <p>Basically I have an <code>application.properties</code> file that contains:</p>
  
  <pre><code>app.datasource.url=jdbc:postgresql://localhost:5432/db_example
  app.datasource.username=myusername
  app.datasource.password=mypassword
  app.datasource.driver-class-name=org.postgresql.Driver
  </code></pre>
  
  <p>Using the <code>psql</code> tool I can normally connect to the <code>db_example</code> database on localhost with the 5432 port and those username and password.</p>
  
  <p>Then I have a <code>MyConfig.java</code> which contains:</p>
  
  <pre><code>package hello;
  
  import javax.sql.DataSource;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Primary;
  
  @Configuration
  public class MyConfig {
  
      @Bean
      @Primary
      @ConfigurationProperties(prefix = "app.datasource")
      public DataSource dataSource() {
          DataSourceBuilder dsb = DataSourceBuilder.create();
          if (dsb == null) {
              return null;
          }
          return dsb.build();
      }
  
  }
  </code></pre>
  
  <p>Then in <code>MainController.java</code> one of the methods that has a <code>@GetMapping</code> annotation, I call the <code>dataSource()</code> method. On the returned object I call <code>getConnection()</code> but that throws an <code>SQLException</code> with message <code>The url cannot be null</code>.</p>
  
  <p>Also in the console the Spring Boot app writes several things:</p>
  
  <pre><code>Not loading a JDBC driver as driverClassName property is null.
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Unable to create initial connections of pool
  </code></pre>
  
  <p>What am I missing? I guess some annotation in <code>MyConfig.java</code>?</p>
  
  <p>EDIT: You might also want to see my <code>pom.xml</code> used to package the thing with <code>mvn</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;gs-mysql-data&lt;/artifactId&gt;
      &lt;version&gt;0.1.0&lt;/version&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.5.8.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) --&gt;
  
          &lt;!--&lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;--&gt;
  
          &lt;!-- Use MySQL Connector-J --&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
              &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
           &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>