Connect Your Payara Cloud Application to a Neon PostgreSQL Database

Payara

Deploying a web application often means connecting to a database. In this guide, I’ll show you how to connect a Jakarta EE application on Payara Cloud to a PostgreSQL database hosted on Neon.

Prerequisites

  • A Jakarta EE sample application (or your own project)
  • A Neon account (the free tier will work)
  • Your Neon database connection details

Get Started With Neon

1. Sample Application If you don’t have one, create a Jakarta EE web application from the Payara Starter page (Web Profile).

2. Create a Neon Account Sign up for a free Neon account. This will provide a default project and database for our example.

3. Get Connection Details Find your database connection details in your Neon dashboard:

  • Database server
  • JDBC URL
  • Database name
  • Database user
  • Database user password

Connect from Your Application

1. Define a Datasource Use the @DataSourceDefinition annotation and MicroProfile Config to set up your datasource:

 

@ApplicationScoped
@DataSourceDefinition(name = "java:app/cloud-postgres",
className = "org.postgresql.ds.PGSimpleDataSource",
url = "${MPCONFIG=DB_URL}",
databaseName = "${MPCONFIG=DB_NAME}",
serverName = "${MPCONFIG=DB_SERVER}",
user = "${MPCONFIG=DB_USER}",
password = "${MPCONFIG=DB_PASSWORD}",
properties = {"targetServerType=primary"})

public class OpenAIFactory {

@Produces
@PersistenceContext
EntityManager entityManager;


}

2. Configure Persistence Update your persistence.xml to use the new datasource:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/persistence"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version="3.0">
<persistence-unit name="pcloud" transaction-type="JTA">

<jta-data-source>java:app/cloud-postgres</jta-data-source>

<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="jakarta.persistence.schema-generation.database.action" value="create"/>
</properties>

</persistence-unit>
</persistence>

3. Add PostgreSQL Driver Include the PostgreSQL JDBC driver as a runtime dependency in your project.

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.1</version>
<scope>runtime</scope>
</dependency>

Deploy to Payara Cloud

1. Create a Payara Cloud Account Sign up and create an application.

2. Set Environment Variables In Payara Cloud’s Configuration, add MicroProfile Config properties matching what you defined in your code (DB_URL, DB_NAME, etc.) using your Neon connection details.

3. Deploy! Click “Deploy” under Revision Actions. Your app will be live and connected to your Neon database.

Troubleshooting Double-check your database connection settings if the deployment fails. Payara Cloud’s logs will usually pinpoint the issue.

That’s it! You’ve successfully connected your Payara Cloud application to a Neon PostgreSQL database. Get the full, detailed guide here for your offline use.

{{cta(‘8d7bedd4-483c-414b-8b1e-47539832ae92’)}}

 

Comments (0)

Post a comment

Your email address will not be published. Required fields are marked *

Payara needs the contact information you provide to us to contact you about our products and services. You may unsubscribe from these communications at any time. For information on how to unsubscribe, as well as our privacy practices and commitment to protecting your privacy, please review our Legal & Privacy Policy.

Related Posts

payara qube logo 5 minutes
Payara

Payara Cloud Is Now part of Payara Qube family of Unified Platforms for Enterprise Java

Payara Cloud is becoming part of Payara Qube family of Java application deployment runtimes. This move reflects how the […]

Payara promotional graphic showing transition from Spring to Jakarta EE, including technology logos, a code icon and arrows leading from Spring to Jakarta EE. 6 minutes
Jakarta EE

From Spring Boot To Jakarta EE 11: How Payara Starter Eases The Transition

If you’ve been living in the Spring ecosystem, you’re used to fast project setup. Spring Initializr gives you a […]

Promotional graphic for a podcast episode titled “Why Open Source is the Future of Business Innovation - A conversation with Arun Gupta”. The podcast is hosted by Payara Community. The right side features a photo of the speaker, Arun Gupta, labeled as a “VP, Developer Experience at JetBrains”. The design uses dark blue and teal backgrounds with coral and fish illustrations. 2 minutes
Community

Payara Podcast – Why Open Source is the Future of Business Innovation – A Conversation with Arun Gupta

Open source is no longer a developer-only concern — it’s at the heart of how modern businesses innovate, build […]