> ## 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).

# DrizzleORM: how to connect to PostgreSQL

# How to Connect Drizzle ORM to PostgreSQL Hosted on Square Cloud

## Introduction

This tutorial will guide you through the steps to connect Drizzle ORM to your PostgreSQL database hosted on Square Cloud.
* First, you need to have an active paid plan on your account. You can view our plans and purchase one according to your needs [here](https://squarecloud.app/pt-br/pricing).
* Next, you will need a PostgreSQL database hosted on Square Cloud. If you haven't created one yet, you can create it [here](https://squarecloud.app/pt-br/dashboard/databases).

## Prerequisites

* Before starting, make sure you have created a database on Square Cloud and connected to it using a management client like DBeaver or another tool to create databases and tables.

![Creating a PostgreSQL database on Square Cloud](https://storage.crisp.chat/users/helpdesk/website/-/d/a/4/9/da49f867735c6000/download_ubih4v.gif)

* You will also need to have Drizzle configured in your project. If you haven't done this yet, you can follow the official Drizzle ORM documentation [here](https://orm.drizzle.team/docs/get-started).

## Connecting Drizzle to PostgreSQL

To connect Drizzle to PostgreSQL, you will need to obtain the connection information: the URL and the certificates.

### Database URL

The database URL you obtain from the connection settings provides the user, password, host, and port for the connection.

```bash
postgresql://username:password@host:port/dbname
```

> **Note:** If you haven't created a specific database, you can use `squarecloud` as the `dbname`.
> postgresql://username:password@host:port/squarecloud

### Certificates

In the database connection settings, download the `certificate.pem` file. We will use this file as `ca`, `key`, and `cert` to connect to the database.

![Downloading the PEM certificate on Square Cloud](https://storage.crisp.chat/users/helpdesk/website/-/d/a/4/9/da49f867735c6000/download_16y38uy.gif)

### Connecting

To establish the connection, you need to configure the `drizzle.config.ts` file. At this stage, you can use the credentials separately.
Configure the connection parameters as shown in the example below:

```ts
// drizzle.config.ts
import { defineConfig } from 'drizzle-kit'
import fs from "fs";
import path from "path";

const fullPath = path.join(process.cwd(), "certs/certificate.pem")
const cert = fs.readFileSync(fullPath, "utf-8")

export default defineConfig({
  dialect: "postgresql",
  dbCredentials: {
    host: "square-cloud-db-{id}.squareweb.app",
    port: 7171,
    user: "squarecloud",
    password: "password",
    database: "squarecloud",
    ssl: {
        ca: cert,
        key: cert,
        cert: cert
    }
  },

  schema: "path/to/schema.ts",
  out: "./drizzle",
  migrations: {
    table: '__drizzle_migrations',
    schema: 'drizzle'
  }
});
```

You also need to configure `client.ts` to use the certificates in a similar manner.

```ts
// client.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
import * as schema from './schema';
import fs from "fs";
import path from "path";

const fullPath = path.join(process.cwd(), "certs/certificate.pem")
const cert = fs.readFileSync(fullPath, "utf-8")

const connection = Pool({
    host: "square-cloud-db-{id}.squareweb.app",
    port: 7171,
    user: "squarecloud",
    password: "password",
    database: "squarecloud",
    ssl: {
        ca: cert,
        key: cert,
        cert: cert
    }
});
export default drizzle({client: connection, schema});
```