Running Quarkus Locally with PostgreSQL in Docker
Setting up a Quarkus application with a PostgreSQL database is a great way to harness the power of reactive programming in Java. In this blog, we’ll walk through the steps to run a Quarkus application locally while connecting it to a PostgreSQL Docker container. We’ll cover how to import the Quarkus project into your IDE, configure PostgreSQL, and test your endpoints.
Prerequisites
Before we dive in, ensure you have the following installed:
Docker
Maven
An IDE (Eclipse or IntelliJ)
Postman (for testing the endpoints)
Step 1: Set Up PostgreSQL in Docker
First, let’s set up the PostgreSQL database using Docker. This is a straightforward process that allows us to run PostgreSQL in an isolated environment.
1. Run PostgreSQL Docker Container
Open your terminal and execute the following command to start a PostgreSQL container:
docker run --name postgres-container -e POSTGRES_PASSWORD=0000 -p 5431:5432 -d postgres
--name postgres-container
: Names your container for easy reference.-e POSTGRES_PASSWORD=0000
: Sets the password for the PostgreSQL superuserpostgres
.-p 5431:5432
: Maps port 5432 inside the container to port 5431 on your local machine.-d
: Runs the container in detached mode.
2. Create a Database
Next, we need to create a database. First, enter the PostgreSQL container:
docker exec -it postgres-container psql -U postgres
Once inside the PostgreSQL shell, create the database:
CREATE DATABASE mydatabase;
Now you can exit the PostgreSQL shell by typing:
\q
Step 2: Import Your Quarkus Project
Now that your PostgreSQL container is set up, let’s import your Quarkus project.
For Eclipse Users
Extract the Quarkus Project ZIP: Unzip the project folder to your desired location.
Open Eclipse: Launch your Eclipse IDE.
Import the Project:
Go to
File
>Import...
.Select
Existing Maven Projects
.Browse to the location where you extracted your Quarkus project.
Select the project and click
Finish
.
For IntelliJ Users
Extract the Quarkus Project ZIP: Unzip the project folder to your desired location.
Open IntelliJ: Launch your IntelliJ IDEA.
Open the Project:
Click on
Open
.Navigate to the extracted folder and select the
pom.xml
file.Click
OK
and thenOpen as Project
.
Step 3: Configure application.properties
The main configuration for your Quarkus application is found in the application.properties
file. Navigate to:
quarkus-postgres-db/src/main/resources/application.properties
Update the File
Replace the content of application.properties
with the following configuration:
# PostgreSQL datasource configuration
quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=postgres
quarkus.datasource.password=0000
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5431/mydatabase
# Hibernate ORM
quarkus.hibernate-orm.database.generation=update
This configuration sets up your application to connect to the PostgreSQL database you created earlier.
Step 4: Run the Quarkus Application
With everything set up, you’re ready to run your Quarkus application.
Execute Maven Commands
In your terminal, navigate to the root directory of your Quarkus project and run the following commands:
Clean the project:
mvn clean
(Optional) If you want to create a JAR file:
mvn clean package
Start the Quarkus application in development mode:
mvn quarkus:dev
Step 5: Testing the Endpoints
Now that your Quarkus application is up and running, you can test the endpoints using Postman.
Available Endpoints
GET /hello: To retrieve a greeting message.
POST /hello/create: To create a new greeting (you may need to provide a JSON body).
GET /hello/list: To list all greetings.
Example Postman Requests
GET Request:
Method: GET
POST Request:
Method: POST
Body (JSON): Keep it empty
GET Request to List:
Method: GET
Conclusion
Congratulations! You’ve successfully set up a Quarkus application with PostgreSQL running in a Docker container. By following these steps, you can now develop and test your application locally with ease.
Feel free to dive deeper into Quarkus and explore its features, such as RESTEasy, Hibernate, and more. Happy coding! If you run into any issues, check out the PostgreSQL Docker documentation or the Quarkus documentation.