Neon Deploy: Learn how Neon helps developers ship faster with Postgres. Grab your ticket
Docs/Getting Started/Quarkus (Reactive)

Connect Quarkus (Reactive) to Neon

Learn how to connect to Neon from Quarkus using a Reactive SQL Client

Quarkus is a Java framework optimized for cloud environments. This guide shows how to connect to Neon from a Quarkus project using a Reactive SQL Client.

To connect to Neon from a Quarkus application:

  1. Create a Neon Project
  2. Create a Quarkus project and add dependencies
  3. Configure a PostgreSQL data source
  4. Use the Reactive PostgreSQL client
  5. Run the application

Create a Neon project

If you do not have one already, create a Neon project.

  1. Navigate to the Projects page in the Neon Console.
  2. Click New Project.
  3. Specify your project settings and click Create Project.

Create a Quarkus project

Create a Quarkus project using the Quarkus CLI:

quarkus create app neon-with-quarkus \
--name neon-with-quarkus \
--package-name com.neon.tech \
--extensions reactive-pg-client,resteasy-reactive

You now have a Quarkus project in a folder named neon-with-quarkus with the Reactive Postgres client and RESTEasy Reactive extensions installed.

Configure a PostgreSQL data source

Create a .env file in the root of your Quarkus project directory. Configure a reactive data source using your Neon database connection string and specifying the database kind as shown:

# Note that "?sslmode=require" is appended to the Neon connection string
QUARKUS_DATASOURCE_REACTIVE_URL=postgresql://[user]:[password]@[neon_hostname]/[dbname]?sslmode=require

note

You can find the connection string for your database in the Connection Details widget on the Neon Dashboard. For more information, see Connect from any application.

Use the Reactive PostgreSQL client

Create a PostgresResource.java file in the same directory as the GreetingResource.java that was generated by Quarkus during project creation. Paste the following content into the PostgresResource.java file:

package com.neon.tech;

import jakarta.inject.Inject;
import io.smallrye.mutiny.Multi;
import io.vertx.mutiny.sqlclient.Row;
import io.vertx.mutiny.sqlclient.RowSet;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/postgres")
public class PostgresResource {
    @Inject
    io.vertx.mutiny.pgclient.PgPool client;

    @GET
    @Path("/version")
    @Produces(MediaType.TEXT_PLAIN)
    public Multi<String> getVersion() {
        return client.query("SELECT version()")
                .execute()
                .onItem().transformToMulti(this::extractVersion);
    }

    private Multi<String> extractVersion(RowSet<Row> rowSet) {
        return Multi.createFrom().iterable(rowSet)
                .map(r -> r.getValue(0).toString());
    }
}

This code defines a HTTP endpoint that will query the database version and return it as a response to incoming requests.

Run the application

Start the application in development mode using the Quarkus CLI from the root of the project directory:

quarkus dev

Visit localhost:8080/postgres/version in your web browser. Your Neon database's Postgres version will be returned. For example:

PostgreSQL 15.4 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit

Need help?

Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more detail, see Getting Support.

Last updated on

Was this page helpful?