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.
- Create the database user to access the database from the spring boot application.
- Set a password for the created user, which was we have using for accessing 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 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.
- Spring boot 2.5.0
- Java 14
- 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.
- 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.
- 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;
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.
- 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
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 –
2 Online Courses | 3 Hands-on Projects | 22+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses