DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. Configuring Spring Boot for PostgreSQL

Configuring Spring Boot for PostgreSQL

In this post, we will learn how to change Spring Boot from the default in-memory H2 to PostgreSQL.

John Thompson user avatar by
John Thompson
·
Jan. 07, 16 · Tutorial
Like (8)
Save
Tweet
Share
263.15K Views

Join the DZone community and get the full member experience.

Join For Free

Spring Boot makes it extremely convenient for programmers to quickly develop Spring applications using an in-memory database, such as H2, HSQLDB, and Derby. These databases are lightweight, easy to use, and emulate other RDBMSs with the help of JPA and Hibernate. Obviously, they don’t provide persistent storage, but they offer a fast way to test persistent functions of your Spring Boot application without going through the hassle of having to install a database server. They are great to use during development when you need to populate your database once your application starts, test your persistent entity mappings, and remove any data when your application ends. To use the embedded databases, you don’t need any special configuration, not even any connection URL. If you are using Maven, you only specify the dependency of the database to use in the POM file. Spring Boot automatically sets up the in-memory database for your use when it finds the database on your classpath.

In-memory databases are useful in the early development stages in local environments, but they have lots of restrictions. As the development progresses, you would most probably require an RDBMS to develop and test your application before deploying it to use a production database server, such as Oracle, MySQL, or PostgreSQL.

Previously, I wrote about creating a web application using Spring Boot and also wrote about configuring Spring Boot to use MySQL for storing the application data. In this post, we will learn how to change Spring Boot from the default in-memory H2 to PostgreSQL, which is one of the most advanced open source database that you will frequently see in production use.

PostgreSQL Configuration

For this post, I’m using PostgreSQL running locally on my laptop. Let’s first start by creating a database for use with Spring Boot. You can do this by using the createdb command line tool. You can locate this tool in the bin folder of your PostgreSQL installation. To create a database named springbootdb, open a command prompt/terminal window and run the following command.

createdb -h localhost -p 5432 -U postgres springbootdb password *********

PostgreSQL Dependency

To use PostgreSQL, you will need the proper database drivers. The required JARs are included in Central Repository of Maven. To include them in your project, you need to add the following dependency (for PostgreSQL 9.2 and later) to your Maven POM file with your specific version.

POM.xml

... 
<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.4-1206-jdbc42</version>
</dependency> 
...

For PostgreSQL up to 9.1, you need to add the following dependency with your specific version.

POM.xml

...
<dependency>
  <groupId>postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.1-901.jdbc4</version>
</dependency>
...

Spring Boot Properties

Our example application is using the H2 database for development. Since H2 is on the classpath, Spring Boot will automatically provide us common sense defaults for the H2 datasource. But this is only if you do not specify another datasource. Thus, by simply providing properties for the PostgreSQL datasource we can override the H2 datasource.

application.properties

spring.datasource.url= jdbc:postgresql://localhost:5432/springbootdb 
spring.datasource.username=postgres spring.datasource.password=postgres@123   
spring.jpa.hibernate.ddl-auto=create-drop

Since, the example web application is using JPA, we configured Hibernate for PostgreSQL in Line 5 to use the create-drop option. This tells Hibernate to recreate the database on startup. However, in testing or production databases, you will want to use the validate option.

Running Spring Boot With PostgreSQL

Once you are done with the preceding configuration, Spring Boot becomes ready for use with PostgreSQL. When you start the project now, the Spring Boot application will use PostgreSQL for the database.

Spring Framework Spring Boot PostgreSQL Database

Published at DZone with permission of John Thompson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Solving the Kubernetes Security Puzzle
  • Testing Level Dynamics: Achieving Confidence From Testing
  • 11 Observability Tools You Should Know
  • Top 5 Data Streaming Trends for 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: