Connecting a SpringBoot Application to a PostgreSQL Database
Hi Spring Lovers😍, Let’s implement a spring boot application. Here, I’m going to connect my application with the PostgreSQL database.
I think you are super excited to try this out😉.
Now, will initiate a spring application. To that access the below link and follow the steps which have been done in the below image of Spring Initializer.
⚓Link to create spring boot demo application using spring initializer.
Here, you have to add dependencies as I have done.
⛄ PostgreSQL Driver SQL → Allows Java programs to connect to a PostgreSQL database using standard, database-independent Java code.
⛄ Spring Data JPA → Persist data in SQL stores with Java Persistence API using Spring Data and Hibernate.
⛄ Spring Web → Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.
🔮Now click on the “GENERATE” button to download the project. Then unzip it and open it using a convenient IDEA. (Ex: IntelliJ)
📝You can download and install the latest PostgreSQL.
Installing Steps of PostgreSQL.
When installing you have to give a password. And keep it in your mind, because that password is needed to connect the server. Furthermore, you have to give the port number for PostgreSQL. Usually, the port number is given as 5432. Now, the app is installed on your local machine.
🔎Then find the app [pgAdmin 4] mentioned below in the image.
Now you can see an interface to enter the password to connect the server as below. Here you have to enter the password you inserted while installing the PostgreSQL onto your machine.
Now you can see a red colour cross mark on PostgreSQL 15. So, we have to connect the database.
Therefore, you can follow the below steps to connect to the server.
Finally, you can see the notifications which show the successful connection of the server in the bottom-right corner of the tab.
For the moment if you don’t have created a database, you can follow the steps mentioned in the image below.
After that, you can see the database and the user of the database in the Statistics tab.
Then we can connect our database with the created spring boot application.
🧾📚Database configuration:
application.properties
file:
spring.datasource.url=jdbc:postgresql:localhost:5432/EmployeeDB //Line 01
spring.datasource.username=postgres //Line 02
spring.datasource.password=1234 //Line 03
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
spring.jpa.open-in-view=false
1️⃣Line 01 → Here, 5432 is given as the port number for PostgreSQL. Your port number may be different. Therefore put your port number there.
The database name <EmployeeD> Balso should be the same as the database you have created while doing the above steps.
2️⃣Line 02 → Username should be yours one.
3️⃣Line 03 → Password also should be yours one.
🔶
spring.jpa.hibernate.ddl-auto=update
update : The object model created based on the mappings (annotations or XML) is compared with the existing schema, and then Hibernate updates the schema according to the diff. It never deletes the existing tables or columns even if they are no more required by the application. [ Further Readings :
spring.jpa.hibernate.ddl-auto
]🔶
spring.jpa.show_sql=true
Tells hibernate to Write all SQL statements to console.
🔶
spring.jpa.properties.hibernate.show_sql=true
Hibernate Configuration Properties [ It represents the type of database used in hibernate to generate SQL statements for a particular relational database. It is used to print the SQL in the log and console.]
🔶
spring.jpa.open-in-view=false
This will disable OSIV so that you can handle the
LazyInitializationException
the right way. OSIV (Open Session in View) is an anti pattern.
😳🤓Wow…! super excited to up the server. we have done. So…, run the application and see.
Tomcat started on port(s): 8080 (http) with context path ''
, you can see this in the terminal.
📌If you want to change port number 8080,server.port=3000
it should be added to the same file.
IDEA with the terminal:
Yeeah…! Finally, we did it.😍😎
Thank you all for following me. Hope to see you soon with another interesting article. If you have any unclear thing, please put a comment here. Until then bye to you…👋👋👋