DEV Community

Tonykaynoni
Tonykaynoni

Posted on

How to connect a SpringBoot Application to PostgreSQL on your windows local machine

Overview
PostgreSQL is a general-purpose and object-relational database management system, and one of the most advanced open-source database system. In this article, we will learn how to link a Spring-Boot application with PostgreSQL.

Image description

Step 1.
Download and install PostgreSQL. You must take note of the details you entered during PostgreSQL installation, it will be used to access your database later.

Step 2.
Search for and Open "SQL shell" on your machine, click enter four times and enter your password if any exist. Create a database using

CREATE DATABASE databaseName;

Image description

Step 3.
Create your spring boot application and add Postgres to the project.

Step 4.
Add the following to your application.properties file,

server.port=9009 //projectPort
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/databasename spring.datasource.username=postgres //postgressUser
spring.datasource.password=yourPostgressPassword
spring.datasource.initialization-mode=always
spring.datasource.initialize=true
spring.datasource.continue-on-error=true
spring.jpa.hibernate.ddl-auto=update

With these steps, Postgres is already included and connected to your project.
You can now use Spring Data JPA repository to save and retrieve data from your database.

To view your project using PgAdmin
Step 1.

Download and install PgAdmin - If you checked PgAdmin during PostgresSql installation skip this step.

Step 2.
Open PgAdmin and Right-click on "servers" and click "create", then "server".

Step 3. 
Give the server a name.

Image description

Step 4.
Navigate to connection on the "create-server panel", and fill your database details as follows.

Image description

Note that, the password is the same with the password used during Postgres installation, and you can check your database Owner(user) by using your SQL shell with the command "\L"

Image description

Step 5 - 
Click save, then you can now view your database on PgAdmin.

Please add your comments, Thanks for reading.

Top comments (0)