/java/

Spring Boot. Switch from h2 to postgresql

2015-12-16 03:47:10

Shipping product can required from us switching form h2 database to Postgresql.

If you have something like this Simple product list & details base on h2.

You need do only few steps to switch your awesome application from h2 to Postgresql.

xml.pom update


<dependency>
<groupid>org.postgresql</groupid>
<artifactid>postgresql</artifactid>
<version>9.4-1201-jdbc41</version>
<scope>runtime</scope>
</dependency>

application.properties update

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

spring.datasource.driver-class-name = org.postgresql.Driver
spring.datasource.url = jdbc:postgresql:btbw
spring.datasource.username=USER_NAME
spring.datasource.password=USER_PASSWORD
spring.datasource.initialize=false

oh boy, and that is everything, spring boot is great now you can create your database and some tables, like this

CREATE SCHEMA btbw_shop_example;
-- \ connect btbw

CREATE USER USER_NAME WITH PASSWORD 'USER_PASSWORD' NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE;

GRANT ALL ON SCHEMA btbw_shop_example TO USER_NAME;
GRANT ALL PRIVILEGES ON DATABASE btbw TO USER_NAME;

-- DROP TABLE IF EXISTS btbw_shop_example.product cascade;

CREATE TABLE btbw_shop_example.product (
  id    serial PRIMARY KEY NOT NULL,
  image varchar(255), name varchar(255) not null,
  price integer not null,
  status varchar(255)
);
ALTER TABLE btbw_shop_example.product OWNER TO USER_NAME;