Every snippet on this page connects to a managed PostgreSQL over TLS and checks that the server is really your database. They are the same snippets the Connect tab of your database shows, where the address, user and certificate path are already filled in.
Replace:
In the snippet
With
HOST, USER
the address and user on the Connect tab
DATABASE
your database name
<password>
the password (Reveal on the Connect tab fills it in for you)
CA_FILE
/etc/zaroz/ca/database-ca.pem in an app running on Zaroz; elsewhere, the full path of the certificate you download on the Connect tab
TLS is required: ssl.ca plus rejectUnauthorized verifies the server.
Through the connection pooler:
Through the pooler in transaction mode set prepare: false.
Prisma
Code
# .env# Prisma 7: the client connects through node-postgres (DATABASE_URL);# the CLI (migrate, db push) through the schema engine (DIRECT_URL).DATABASE_URL="postgresql://USER:<password>@HOST:5432/DATABASE?sslmode=verify-full&sslrootcert=CA_FILE"DIRECT_URL="postgresql://USER:<password>@HOST:5432/DATABASE?sslmode=require&sslcert=CA_FILE&sslaccept=strict"// prisma.config.tsimport "dotenv/config";import { defineConfig, env } from "prisma/config";export default defineConfig({ schema: "prisma/schema.prisma", datasource: { url: env("DIRECT_URL") },});// your appimport { PrismaPg } from "@prisma/adapter-pg";import { PrismaClient } from "./generated/prisma/client";const prisma = new PrismaClient({ adapter: new PrismaPg({ connectionString: process.env.DATABASE_URL }),});// Prisma 6: schema.prisma keeps url = env("DIRECT_URL") and needs no adapter.
TLS is required: through the adapter, node-postgres verifies the server with sslmode=verify-full and sslrootcert; the CLI uses sslcert with sslaccept=strict, without which Prisma accepts any certificate.
Give sslcert an absolute path: a relative one is resolved from the prisma folder, not from where you run the command.
Through the connection pooler:
Through the pooler in transaction mode, Prisma 6 needs &pgbouncer=true on its URL; the Prisma 7 adapter needs nothing.
Drizzle ORM
Code
import fs from "node:fs";import { drizzle } from "drizzle-orm/node-postgres";import { sql } from "drizzle-orm";import pg from "pg";const pool = new pg.Pool({ host: "HOST", port: 5432, user: "USER", password: "<password>", database: "DATABASE", ssl: { ca: fs.readFileSync("CA_FILE", "utf8") },});const db = drizzle(pool);console.log((await db.execute(sql`SELECT 1 AS ok`)).rows);await pool.end();
TLS is required: Drizzle uses node-postgres, so the CA bundle goes in ssl.ca.
TLS is required: sslmode=verify-full with sslrootcert verifies the server; the CA file must be PEM.
JDBC does not read PGSSLROOTCERT, so sslrootcert stays in the URL even inside Zaroz.
Npgsql
Code
using Npgsql;var connectionString = "Host=HOST;Port=5432;Database=DATABASE;Username=USER;Password=<password>;SSL Mode=VerifyFull;Root Certificate=CA_FILE";await using var connection = new NpgsqlConnection(connectionString);await connection.OpenAsync();await using var command = new NpgsqlCommand("SELECT 1", connection);Console.WriteLine(await command.ExecuteScalarAsync());
TLS is required: SSL Mode=VerifyFull with Root Certificate verifies the server.
Rust
sqlx
Code
use sqlx::postgres::{PgConnectOptions, PgSslMode};use sqlx::Connection;#[tokio::main]async fn main() -> Result<(), sqlx::Error> { let options = PgConnectOptions::new() .host("HOST") .port(5432) .username("USER") .password("<password>") .database("DATABASE") .ssl_mode(PgSslMode::VerifyFull) .ssl_root_cert("CA_FILE"); let mut connection = sqlx::PgConnection::connect_with(&options).await?; let one: i32 = sqlx::query_scalar("SELECT 1").fetch_one(&mut connection).await?; println!("{one}"); Ok(())}
TLS is required: PgSslMode::VerifyFull with ssl_root_cert verifies the server.
Enable a TLS feature of sqlx (tls-rustls or tls-native-tls) next to runtime-tokio.
tokio-postgres
Code
use native_tls::{Certificate, TlsConnector};use postgres_native_tls::MakeTlsConnector;#[tokio::main]async fn main() -> Result<(), Box<dyn std::error::Error>> { let ca = Certificate::from_pem(&std::fs::read("CA_FILE")?)?; let tls = MakeTlsConnector::new(TlsConnector::builder().add_root_certificate(ca).build()?); let (client, connection) = tokio_postgres::connect( "host=HOST port=5432 user=USER password=<password> dbname=DATABASE sslmode=require", tls, ) .await?; tokio::spawn(connection); let row = client.query_one("SELECT 1", &[]).await?; println!("{}", row.get::<_, i32>(0)); Ok(())}
TLS is required: sslmode=require makes tokio-postgres insist on TLS; the native-tls connector verifies the server against the CA bundle and the host name.