title: Problems with Postgresql with Spring Boot on Heroku
body: <p>I decided to try out Spring Boot on Heroku and everything works perfectly...well, apart from the database!</p>
  
  <p>I tried many different things to make it work, followed a lot of different approaches I found online and also from questions on StackOverflow, but nothing seems to work for me.
  I'm going to post only the bare minimum of the app I'm trying to deploy.</p>
  
  <p>The below is desployed fine, but when I'm trying to POST something I get the following as response:</p>
  
  <p><code>{"timestamp":1413600470146,"error":"Unsupported Media Type","status":415,"message":"Unsupported Media Type"}</code></p>
  
  <p>And when I'm trying to GET, I get the following exception in the Heroku logs:
  nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause</p>
  
  <pre><code>org.postgresql.util.PSQLException: ERROR: relation "exampleEntity" does not exist
    Position: 109
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    ....
  </code></pre>
  
  <p>So, I have the following pom.xml:</p>
  
  <pre><code>&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;com.example&lt;/groupId&gt;
      &lt;artifactId&gt;project-name&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&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.0.2.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;maven.compiler.source&gt;1.7&lt;/maven.compiler.source&gt;
          &lt;maven.compiler.target&gt;1.7&lt;/maven.compiler.target&gt;
          &lt;jersey.version&gt;2.8&lt;/jersey.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencyManagement&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.glassfish.jersey&lt;/groupId&gt;
                  &lt;artifactId&gt;jersey-bom&lt;/artifactId&gt;
                  &lt;version&gt;${jersey.version}&lt;/version&gt;
                  &lt;type&gt;pom&lt;/type&gt;
                  &lt;scope&gt;import&lt;/scope&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/dependencyManagement&gt;
  
      &lt;dependencies&gt;
          &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;
          &lt;dependency&gt;
              &lt;groupId&gt;postgresql&lt;/groupId&gt;
              &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
              &lt;version&gt;9.1-901-1.jdbc4&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;
              &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.ext&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-spring3&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-media-moxy&lt;/artifactId&gt;
          &lt;/dependency&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;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.test-framework.providers&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-test-framework-provider-inmemory&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&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;dependency&gt;
              &lt;groupId&gt;org.assertj&lt;/groupId&gt;
              &lt;artifactId&gt;assertj-core&lt;/artifactId&gt;
              &lt;version&gt;1.5.0&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.skyscreamer&lt;/groupId&gt;
              &lt;artifactId&gt;jsonassert&lt;/artifactId&gt;
              &lt;version&gt;1.2.2&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&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;repositories&gt;
          &lt;repository&gt;
              &lt;id&gt;spring-releases&lt;/id&gt;
              &lt;name&gt;Spring Releases&lt;/name&gt;
              &lt;url&gt;http://repo.spring.io/libs-release&lt;/url&gt;
          &lt;/repository&gt;
          &lt;repository&gt;
              &lt;id&gt;org.jboss.repository.releases&lt;/id&gt;
              &lt;name&gt;JBoss Maven Release Repository&lt;/name&gt;
              &lt;url&gt;https://repository.jboss.org/nexus/content/repositories/releases&lt;/url&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
  
      &lt;pluginRepositories&gt;
          &lt;pluginRepository&gt;
              &lt;id&gt;spring-releases&lt;/id&gt;
              &lt;name&gt;Spring Releases&lt;/name&gt;
              &lt;url&gt;http://repo.spring.io/libs-release&lt;/url&gt;
          &lt;/pluginRepository&gt;
      &lt;/pluginRepositories&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Then my Main class is:</p>
  
  <pre><code>package com.example;
  
  import org.glassfish.jersey.servlet.ServletContainer;
  import org.glassfish.jersey.servlet.ServletProperties;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.boot.builder.SpringApplicationBuilder;
  import org.springframework.boot.context.embedded.ServletRegistrationBean;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  
  import com.example.config.JerseyConfig;
  
  @EnableAutoConfiguration
  @ComponentScan
  @EnableJpaRepositories
  public class Main {
  
      @Bean
      public ServletRegistrationBean jerseyServlet() {
          ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*");
          // our rest resources will be available in the path /rest/*
          registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
          return registration;
      }
  
      public static void main(String[] args) {
          new SpringApplicationBuilder(Main.class).showBanner(false).run(args);
      }
  
  }
  </code></pre>
  
  <p>I also have a JerseyConfig (for Jersey configuration):</p>
  
  <pre><code>package com.example.config;
  
  import org.glassfish.jersey.filter.LoggingFilter;
  import org.glassfish.jersey.server.ResourceConfig;
  import org.glassfish.jersey.server.spring.scope.RequestContextFilter;
  
  public class JerseyConfig extends ResourceConfig {
  
      public JerseyConfig() {
          register(RequestContextFilter.class);
          packages("com.example");
          register(LoggingFilter.class);
      }
  }
  </code></pre>
  
  <p>And a BasicDataSource for the database:</p>
  
  <pre><code>package com.example.config;
  
  import java.net.URI;
  import java.net.URISyntaxException;
  
  import javax.sql.DataSource;
  
  import org.apache.commons.dbcp.BasicDataSource;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  @Configuration
  public class SimpleDbConfig {
  
      @Bean
      public DataSource dataSource() throws URISyntaxException {    
          URI dbUri;
          try {
              String username = "username";
              String password = "password";
              String url = "jdbc:postgresql://localhost/dbname";
              String dbProperty = System.getProperty("database.url");
              if(dbProperty != null) {
                  dbUri = new URI(dbProperty);
  
                  username = dbUri.getUserInfo().split(":")[0];
                  password = dbUri.getUserInfo().split(":")[1];
                  url = "jdbc:postgresql://" + dbUri.getHost() + dbUri.getPath();
              }     
  
              BasicDataSource basicDataSource = new BasicDataSource();
              basicDataSource.setUrl(url);
              basicDataSource.setUsername(username);
              basicDataSource.setPassword(password);
              return basicDataSource;
  
          } catch (URISyntaxException e) {
              //Deal with errors here.
              throw e;
          }
      }
  }
  </code></pre>
  
  <p>Finally, I have my REST resource:</p>
  
  <pre><code>package com.example.resource;
  
  import javax.ws.rs.Consumes;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  
  import com.example.service.ExampleService;
  import com.example.transport.ExampleEntity;
  
  @Path("/entities")
  @Component
  public class ExampleResource {
  
      private ExampleService exampleService;
  
      @Autowired
      public ExampleResource(ExampleService exampleService) {
          this.exampleService = exampleService;
      }
  
      @POST
      @Path("/new")
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      public ExampleEntity createNewEntity(ExampleEntity entity) {
          return exampleService.createNewEntity(entity);
      }
  
      @GET
      @Path("/{id}")
      @Produces(MediaType.APPLICATION_JSON)
      public ExampleEntity getExampleDetails(@PathParam("id") Long id) {
          return exampleService.findEntity(id);
      }
  
  }
  </code></pre>
  
  <p>A service class:</p>
  
  <pre><code>package com.example.service;
  
  import javax.transaction.Transactional;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import com.example.repositories.ExampleRepository;
  import com.example.transport.ExampleEntity;
  
  @Service
  public class ExampleService {
  
      private ExampleRepository repo;
  
      @Autowired
      public ExampleService(ExampleRepository repo) {
          this.repo = repo;
      }
  
      @Transactional
      public ExampleEntity createNewEntity(ExampleEntity entity) {
          ExampleEntity savedEntity = repo.save(entity);
          return savedEntity;
      }
  
      @Transactional
      public ExampleEntity findEntity(Long id) {
          return repo.findOne(id);
      }
  
  }
  </code></pre>
  
  <p>The entity:</p>
  
  <pre><code>package com.example.transport;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  @Entity
  public class ExampleEntity {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private long id;
  
      private String name;
  
      public long getId() {
          return id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p>And finally the repo interface:</p>
  
  <pre><code>package com.example.repositories;
  
  import org.springframework.data.repository.CrudRepository;
  
  import com.example.transport.ExampleEntity;
  
  public interface MeetingRepository extends CrudRepository&lt;ExampleEntity, Long&gt; {
  
  }
  </code></pre>
  
  <p>Any help is much appreciated!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>The Procfile I'm using is the following:</p>
  
  <pre><code>web: java -Dserver.port=$PORT -Ddatabase.url=$DATABASE_URL $JAVA_OPTS -jar target/project-name-0.0.1-SNAPSHOT.jar
  </code></pre>