0

I am trying to expose a Spring JPA repository on Postgresql as restful service/apis. I am using Spring boot started kit.

I am following this example https://spring.io/guides/gs/accessing-data-rest/

This is using H2 as embedded database. I am able to run this example and use the rest calls.

Now i want to migrate this same example from H2 to Postgresql. For this i removed the H2 dependency and added postgresql dependency in the pom.xml.

Also added below properties in application.properties to congifure the postgreql instance.

spring.datasource.url=jdbc:postgresql:employee
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=postgres
spring.datasource.password=admin
spring.data.jpa.repositories.enabled=true
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.database=postgresql
spring.jpa.hibernate.ddl-auto=validate

logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

Below is my pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.saurav.cf.casestudy</groupId>
    <artifactId>employeerest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>employeerest</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <start-class>org.saurav.cf.casestudy.employee.EmployeerestApplication</start-class>
    </properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
                <dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0.2</version>
</dependency>

<dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
      </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

I have already created a DB called employee in my running postgresql instance and modified the entities and rest mvc annonations accordingly.

Now when i am trying to execute the apis. It is executing and but seems to be executing on the H2 database not on the running postgreql instance.

How to disable the H2 databse and have the postgresql come in ?

Best Regards, Saurav

saurav
  • 4,032
  • 7
  • 38
  • 74

1 Answers1

0

It seems everything was fine.

I was not giving the correct schema name for my db.

My spring app was by defeault creating a table under the public schema of postgresql.

How to specify schema in the jdbc postgresql url, check this link Is it possible to specify schema when connecting to postgres with JDBC?

Community
  • 1
  • 1
saurav
  • 4,032
  • 7
  • 38
  • 74