EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial spring boot postgresql
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

spring boot postgresql

spring boot postgresql

Introduction to spring boot postgresql

Spring boot postgresql shows how we are using PostgreSQL database with application in spring boot, as we know that spring boot is a very popular Java framework used to develop the enterprise application. We can also develop the enterprise application using spring boot and PostgreSQL database; we can also develop the standalone as well as web-based application by using PostgreSQL. To use PostgreSQL in the database, we need to first install it on the same or another server. We have using the JDBC template to connect the PostgreSQL database using spring boot.

What is spring boot postgresql?

  • Using spring boot, we can easily connect to the database server. Unfortunately, PostgreSQL is running by default on the port as 5432. I suppose we want to change the port; we need to change it in the configuration file.
  • We can develop the web application by using PostgreSQL, which was doing the CRUD operations on the PostgreSQL database.
  • To use the database and table in our database, we need to first create it on the database server; after creating it on the PostgreSQL database server, we can use the same in our PostgreSQL application.
  • To use the PostgreSQL database in our application, we need to do the following things on the database server as follows.
  1. Create the database user to access the database from the spring boot application.
  2. Set a password for the created user, which was we have using for accessing the application.
  3. Create the database for the spring boot application.
  4. Give the privileges of users on the database which was we have created to access the application.
  • We need to use a spring boot starter to use the PostgreSQL database in our application. First, the spring boot starter parent will contain the configuration of the application. Then, the starter web is used to build the web page.
  • PostgreSQL application uses tomcat as the default container while developing an application. Therefore, at the time of developing the application with the PostgreSQL database, we need to add the dependency in the pom.xml file.
  • Also, we need to write multiple configurations related to the postgresql project in configuration files. Finally, we have added the postgresql dependency in our application to use the PostgreSQL database with the application.

Using PostgreSQL in Spring Boot

  • To develop an application with a PostgreSQL database, we need the following prerequisites are as follows.
  1. Spring boot 2.5.0
  2. Java 14
  3. PostgreSQL 12
  • There are multiple PostgreSQL versions available; we can use any version while developing an application.
  • We have using the spring JDBC and JDBC template to connect the PostgreSQL database server. Also, we can use JPA of spring data to connect the database of PostgreSQL.
  • Also, we need to add the JDBC driver dependency of the PostgreSQL database to allow the spring boot application to connect or talk with the PostgreSQL database server.
  • After adding the PostgreSQL JDBC dependency, we need to add the properties of the data source to use the connection information of the database.
  • We have to add the PostgreSQL dependency in the pom.xml file in the project of the PostgreSQL application.
  • We can also use the spring data JPA for advanced use in the project.
  • To develop the application using PostgreSQL, we first need to add the dependency in our project. Then, after adding a dependency, we need to configure the properties of the data source.

Spring boot PostgreSQL setup

Below example shows to set up PostgreSQL application are as follows. The below step shows examples to set up an application is as follows.

  • Create a project template using a spring initializer and give the following name to the project metadata.

Group – com.example
Artifact name – spring-boot-postgresql
Name – spring-boot- postgresql
Description - Project of spring-boot- postgresql
Package name - com.example.spring-boot- postgresql
Packaging – Jar
Java – 11
Dependencies – spring web, PostgreSQL driver.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

spring boot postgresql output 1

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • After generating project extract files and open this project by using the spring tool suite 

In this step, we have to extract our PostgreSQL project and opening in the spring tool suite.

spring boot postgresql output 2

  • After opening the project using the spring tool suite, check the project and its files. 

In this step, we are checking the postgresql project and its file. But, first, we have to check all the structures of the project.

spring boot postgresql output 3

  • After checking the project, create a database and user 

In this step, we create the user name as spring-boot and the database name as springbootpost to connect the database server.

# Create user springboot with password 'postgres' SUPERUSER;
# create database springbootpost;

spring boot postgresql output 4

Spring Boot PostgreSQL example

Below steps shows examples of PostgreSQL are as follows.

  • Add the dependency 

The first step to create an example is to add the PostgreSQL dependency in the pom.xml file.

Code 

<dependency>   -- Start of dependency tag.
<groupId>org.postgresql</groupId>   -- Start and end of groupId tag.
<artifactId>postgresql</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.

output 5

  • Configure data source properties

Code 

spring.datasource.url=jdbc:postgresql://localhost:5432/springbootpost
spring.datasource.username=springboot
spring.datasource.password=postgres

output 6

  • Connect the PostgreSQL database using spring boot

Code 

<dependency>   -- Start of dependency tag.
<groupId>org.springframework.boot</groupId>   -- Start and end of groupId tag.
<artifactId>spring-boot-starter-jdbc</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.

output 7

  • Develop code to insert a row in the PostgreSQL table 

Code 

public class springbootpostgresql implements CommandLineRunner {
@Autowired
private JdbcTemplate jdbcTemplate;
public static void main /* main method */ (String[] args) {
SpringApplication.run (springbootpostgresql.class, args);
}
@Override
public void run(String... args) throws Exception {
String sql = "INSERT INTO stud (id, name) VALUES ("
+ "1, 'ABC')";
int rows = jdbcTemplate.update (sql);
if (rows > 0) {
System.out.println ("One row inserted.");    }

output 8

  • Run the postgresql application

output 9

  • Check the inserted row from the postgresql application

Code 

# select * from stud;

output 10

Conclusion

Spring boot PostgreSQL is a very popular Java framework that was used to develop an enterprise application. Postgresql is showing that how we are using the PostgreSQL database with the application. For example, we can develop a web application by using PostgreSQL, which was doing the CRUD operations.

Recommended Articles

This is a guide to spring boot PostgreSQL. Here we discuss how we are using the PostgreSQL database with application in spring boot. You may also have a look at the following articles to learn more –

  1. PostgreSQL Rename Database
  2. PostgreSQL encode
  3. PostgreSQL List Databases
  4. PostgreSQL Merge
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more