Updated May 30, 2023
Introduction to spring boot postgresql
Spring boot postgresql shows how we use the PostgreSQL database with applications 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 and standalone and web-based applications using PostgreSQL. To use PostgreSQL in the database, we must first install it on the same or another server. We use the JDBC template to connect the PostgreSQL database.
What is spring boot postgresql?
- Using spring boot, we can easily connect to the database server. Unfortunately, PostgreSQL is running by default on port 5432. I suppose we want to change the port; we need to change it in the configuration file.
- We can develop the web application using PostgreSQL, which does the CRUD operations on the PostgreSQL database.
- To use the database and table in our database, we need to 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.
- Create the database user to access the database from the spring boot application.
- Please set a password for the created user, which we used to access the application.
- Create the database for the spring boot application.
- Give the privileges of users on the database which was we have created to access the application.
- We need a spring boot starter to use the PostgreSQL database in our application. First, the spring boot starter parent will contain the application’s configuration. Then, the starter web is used to build the web page.
- PostgreSQL application uses Tomcat as the default container while developing an application. Therefore we need to add the dependency in the pom.xml file.
- When developing the application with the PostgreSQL databaseAlso, 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.
- Spring boot 2.5.0
- Java 14
- PostgreSQL 12
- Multiple PostgreSQL versions are available; we can use any version while developing an application.
- We use the spring JDBC and JDBC template to connect the PostgreSQL database server. Also, we can use the 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.
- The PostgreSQL application project’s pom.xml file has to include a dependency for PostgreSQL.
- We can also use the spring data JPA for advanced use in the project.
- We first need to add the dependency in our project to develop the application using PostgreSQL. Then, after adding a dependency, we need to configure the data source’s properties.
Setup
The below example shows to set up the PostgreSQL application are as follows. The below step shows examples to set up an application 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.
- After generating project extract files and open this project by using the spring tool suite
In this step, we must extract our PostgreSQL project and opening in the spring tool suite.
- 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.
- 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;
Example
The below steps show examples of PostgreSQL as follows.
- Add the dependency
The first step to creating 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.
- Configure data source properties
Code
spring.datasource.url=jdbc:postgresql://localhost:5432/springbootpost
spring.datasource.username=springboot
spring.datasource.password=postgres
- 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.
- 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."); }
- Run the postgresql application
- Check the inserted row from the postgresql application
Code
# select * from stud;
Conclusion
It is a popular Java framework used to develop an enterprise application. Postgresql shows how we are using the PostgreSQL database with the application. For example, we can develop a web application using PostgreSQL, which performs CRUD operations.
Recommended Articles
We hope that this EDUCBA information on “spring boot postgresql” was beneficial to you. You can view EDUCBA’s recommended articles for more information.