JavaDream javadream Spring boot Postgres

Spring boot Postgres

In this blog we ‘ll learn about spring boot PostgreSQL. We know that PostgreSQL is best if we have big projects along with complex database structure. PostgreSQL is commonly used for heavy writing.

In this blog we ‘ll cover the items given as below. If you want to skip any section or want to read a specific section you can directly jump into that, Please follow the Topics list.

And If you want to see code only you can skip and directly jump into the code on Github

Topics

  • What is PostgreSQL
  • Install PostgreSQL on windows system [pgadmin 4 download for windows]
  • Spring boot PostgreSQL integration

What is PostgreSQL

PostgreSQL is an open source relational database management systems. PostgreSQL is also know as Postgres. As we have mentioned above, We use PostgreSQL when we have to perform more complex write operations. Postgres is designed in a way that it can handle a wide range of load, So if our application is big and we have more database data to handle we must opt for the PostgreSQL database.

Some of the beneficial operation of PostgreSQL are as given below:

  • ACID Feature
  • Concurrency
  • Full Text Search
  • Internationalization
  • Database Replication and High availability

Install PostgreSQL on windows system

Installing PostgreSQL on windows system is very easy. In this blog we are downloading PostgreSQL on windows, If you are having any other operating system just follow the process given on this link Postgres Download.

Installing PostgreSQL on windows system follow the steps given below:

  • Go to the PostgreSQL download page for windows https://www.postgresql.org/download/windows/
  • Click on the Download the Installer Link
  • After clicking on the Download the Installer Link a page ‘ll open with all version of PostgreSQL. Just choose what version you want to download.
  • Click on download icon and your download ‘ll be started. After this go to your windows download section and see the PostgreSQL installer.
  • Double click on this Installer and start the installation process.
  • Just follow the process of the installation.

Process ‘ll take time so be patient. We have attached the complete installation process screenshot. If you face any issue at the time of PostgreSQL installation. Please follow the screenshots it ‘ll help you.

PostgreSQL download page
PostgreSQL specific version download page
PostgreSQL download

Just click on Finish. So till here if everything goes good you have your PostgreSQL ready in your windows system.

To verify that PostgreSQL successfully installed, Press Window button on your keyboard and search for pgadmin. If you see output like below congratulations you have your PostgreSQL installed successfully. Open this pgAdmin 4 console.

Or the other way to verify that you have successfully installed PostgreSQL. Go to this location on your windows system C:\Program Files\PostgreSQL\17\bin. If you have downloaded any other version your path might be different after \PostgreSQL. Open this path in cmd(Command Prompt). After this to verify PostgreSQL installation you have to write below command in your cmd console.

psql -U postgres

If you remember while we installing PostgreSQL. We have set password for the postgres sql. Just provide that password and you ‘ll see the PostgreSQL console. Write command to check installed version

select version();

So we have our PostgreSQL setup ready in our Window systems. If you have any other operating system i am hoping you also done with PostgreSQL installation for your system.

So let’s move to the final part of this blog i.e. Spring boot PostgreSQL integration.

Spring boot PostgreSQL integration

spring boot PostgreSQL integration is very easy and straightforward. We just have to follow below steps and in next 5 minute we ‘ll be ready with our spring boot application with having PostgreSQL connectivity.

Steps to Follow:

  • Create a Spring boot project via start.spring.io or if you have spring boot plugin in your IDE, You can create the project from IDE itself.
  • Add the required dependency of JPA and PostgreSQL in the pom.xml file.
  • We have to add some property for making PostgreSQL connection into application.properties file
  • We ‘ll create a Entity class. As we are using JPA(Java Persistence API) in our project. It ‘ll automatically map our Entity class to a table in PostgreSQL database.
  • Run Spring boot project and test the connectivity with PostgreSQL

Project Creation using start.spring.io

Project Structure:

Start Coding

In this project we ‘ll see how we can connect our spring boot application to PostgreSQL. So for this we ‘ll create an entity class and when we run our application this entity class should be converted in a table in PostgreSQL database.

Follow below steps:
  • Add Below dependency in you pom.xml file.
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <scope>runtime</scope>
 <optional>true</optional>
</dependency>

<dependency>
 <groupId>org.postgresql</groupId>
 <artifactId>postgresql</artifactId>
 <scope>runtime</scope>
</dependency>Code language: HTML, XML (xml)
  • We need to add below properties in our application.properties file
spring.application.name=SpringbootWithPostgreSQL

# PostgreSQL Configuration
spring.datasource.url=jdbc:postgresql://localhost:5432/javadream?currentSchema=vasu
spring.datasource.username=postgres
spring.datasource.password=postgres

# Hibernate Configuration
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImplCode language: PHP (php)
  • Create a Entity class. This Entity class ‘ll automatically converted into a table in PostgreSQL database by JPA.
package com.javadream.SpringbootWithPostgreSQL.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "employee")
public class Employee {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	private String name;
	
	@Column(name = "\"lastName\"")
	private String lastName;
	
	@Column(name = "\"mobileNumber\"")
	private String mobileNumber;

	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;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getMobileNumber() {
		return mobileNumber;
	}

	public void setMobileNumber(String mobileNumber) {
		this.mobileNumber = mobileNumber;
	}

}
Code language: JavaScript (javascript)
  • No need to change your main class. Let it be same
package com.javadream.SpringbootWithPostgreSQL;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootWithPostgreSqlApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootWithPostgreSqlApplication.class, args);
	}

}Code language: JavaScript (javascript)

Run your application and after application started just see the console logs. As we are using spring.jpa.show-sql=true property it should write the log of table creation for Employee entity class.

Now go to your pgAdmin and see there. Employee table has been created there.

postgres table creation using spring boot
pgAdmin4 schema and table creation
Tip:

So when we do the Installation for PostreSQL on windows, It automatically covers the pgadmin 4 download for windows. Because PostgreSQL contains pgAdmin4 we don’t need to install it explicitly.

Checkout Complete Code from Github

Complete Code

See More Blog and enhance you Tech skills

Spring boot integration with Twilio for sending SMS

Spring boot H2 Database

JPA Mapping Short Notes

Share with others

Related Post