> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://help.squarecloud.app/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# JDBC: how to connect to PostgreSQL (Java, .pk8)

# 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](https://storage.crisp.chat/users/helpdesk/website/-/d/a/4/9/da49f867735c6000/download_15pparp.gif)

## 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](https://storage.crisp.chat/users/helpdesk/website/-/d/a/4/9/da49f867735c6000/download_1ij5qkb.gif)

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:
```bash
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:
```env
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"
```