Articles on: Database & Storage

JDBC: How to connect to PostgreSQL on Square Cloud

How to Connect to PostgreSQL with JDBC on Square Cloud


This tutorial will guide you through the steps to connect to a PostgreSQL database using JDBC (Java Database Connectivity). JDBC is a Java API that allows you to connect and execute queries on databases.


Prerequisites


Before starting, make sure you have created a database on Square Cloud and connected to a management client, such as DBeaver, to create your data structure.


Creating a PostgreSQL database on Square Cloud


Steps to Connect to PostgreSQL with JDBC


1. Obtain Your PostgreSQL Connection Details


To connect your Java application to the PostgreSQL database, you need to obtain your access credentials:

  • Connection String: The URL used to connect to your PostgreSQL database.
  • Certificate: The SSL certificate files to ensure secure connections. You will need the .crt and .key files.


Downloading the PEM certificate on Square Cloud


You can find these details in the Square Cloud dashboard, under the Databases section. Click on your PostgreSQL database to view the details, and then go to the Configuration tab to find the connection string.

Note: If you did not copy the password when you created the database, you will need to regenerate the password in the dashboard and paste it into the URL.


2. Create the .pk8 File


The JDBC driver requires the SSL key certificate to be in a specific format: a .pk8 file. You can create this file using the OpenSSL utility.


To convert the .key file downloaded from the dashboard to the .pk8 format, run the following command in your terminal:

openssl pkcs8 -topk8 -inform PEM -in path/to/private-key.key -outform DER -out path/to/create/private-key.pk8 -nocrypt


Remember to replace path/to/private-key.key and path/to/create/private-key.pk8 with the actual paths on your system. Save the generated private-key.pk8 file in a folder along with the other required certificates (for example, in a folder named certs).


3. Update the DATABASE_URL


Finally, you must update your .env file to include the connection string and the newly created SSL configuration.


In your Java project, locate the .env file and update the DATABASE_URL variable with your PostgreSQL connection string. Make sure to include the SSL configuration pointing to the certificates. The final structure should look like this:

DATABASE_URL="jdbc:postgresql://host:port/database?sslrootcert=path/to/ca-certificate.crt&sslcert=path/to/client-certificate.crt&sslkey=path/to/private-key.pk8"

Updated on: 05/21/2026

Was this article helpful?

Share your feedback

Cancel

Thank you!