Articles on: Database & Storage

Drizzle ORM: How to connect to PostgreSQL on Square Cloud

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.
  • Next, you will need a PostgreSQL database hosted on Square Cloud. If you haven't created one yet, you can create it here.


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


  • 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.


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.


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


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:


// 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.


// 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});

Updated on: 05/21/2026

Was this article helpful?

Share your feedback

Cancel

Thank you!