Zaroz
Databases

Connect an app to a managed database

TLS in plain words

Traffic between your app and a Zaroz database is always encrypted. Your app also checks that the server is really your database, so nobody in between can read the data or pretend to be the database. That is TLS. We require it on every connection.

What you need to do depends on where your app runs:

  • On Zaroz (an app or a worker in any of your projects): nothing. Every Zaroz container already trusts the Zaroz database certificate.
  • Anywhere else (your laptop, another cloud): download the certificate once, then point your client at it with the setting shown below for your language.

From a Zaroz app: connect in one step

Open the database, choose Connect to an app, and pick the app. Zaroz adds these variables to the app and restarts it:

VariableValue
DATABASE_URLpostgresql://USER:PASSWORD@db-NAME:5432/DATABASE?sslmode=verify-full
PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORDthe same parts, for clients that read them

They are linked, not copied. When you rotate the password or the address changes, Zaroz updates the app on its next restart. In the app's Environment tab they show as Linked ยท NAME. Choose Unlink to turn one into a plain value you edit yourself.

You can also link a variable from the app's side. In Add variable, type a name such as DATABASE_URL and pick the suggestion, or open Insert from your project.

Every Zaroz container has the certificate at /etc/zaroz/ca/database-ca.pem. Zaroz also sets:

VariablePoints atUsed by
NODE_EXTRA_CA_CERTS/etc/zaroz/ca/database-ca.pemNode.js (node-postgres, Prisma's Node driver adapters)
PGSSLROOTCERT/etc/zaroz/ca/ca-bundle.pemlibpq: psycopg, PHP PDO, pgx, psql
ZAROZ_CA_DIR/etc/zaroz/cayour own scripts

ca-bundle.pem holds the usual public certificate authorities plus Zaroz's, so a connection to a database outside Zaroz keeps working too. If you set one of these variables yourself, your value wins.

Snippets that work inside Zaroz

JavascriptCode
// node-postgres import pg from "pg"; const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
Code
// Prisma: schema.prisma, DATABASE_URL as Zaroz sets it datasource db { provider = "postgresql" url = env("DATABASE_URL") }
Code
# psycopg 3 import os, psycopg conn = psycopg.connect(os.environ["DATABASE_URL"]) # SQLAlchemy from sqlalchemy import create_engine engine = create_engine(os.environ["DATABASE_URL"].replace("postgresql://", "postgresql+psycopg://", 1))
GoCode
// pgx pool, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))
JavaCode
// JDBC (pgjdbc does not read PGSSLROOTCERT, so name the file) String url = "jdbc:postgresql://" + System.getenv("PGHOST") + ":" + System.getenv("PGPORT") + "/" + System.getenv("PGDATABASE") + "?sslmode=verify-full&sslrootcert=/etc/zaroz/ca/database-ca.pem"; Connection c = DriverManager.getConnection(url, System.getenv("PGUSER"), System.getenv("PGPASSWORD"));
Code
// PHP PDO $pdo = new PDO( sprintf("pgsql:host=%s;port=%s;dbname=%s;sslmode=verify-full", getenv("PGHOST"), getenv("PGPORT"), getenv("PGDATABASE")), getenv("PGUSER"), getenv("PGPASSWORD"), );

From outside Zaroz

  1. Turn on Public access for the database. Its address is NAME.db.zone.ZONE.zaroz.cloud.
  2. Download the certificate from the database's Connect tab (Download certificate). Save it next to your app, for example as zaroz-ca.pem.
  3. Use the setting for your client, with the full path to the file:
ClientSetting
node-postgres?sslmode=verify-full&sslrootcert=/path/to/zaroz-ca.pem, or NODE_EXTRA_CA_CERTS=/path/to/zaroz-ca.pem
Prisma?sslmode=require&sslcert=/path/to/zaroz-ca.pem&sslaccept=strict
psycopg, SQLAlchemy, psql, PHP PDOsslmode=verify-full sslrootcert=/path/to/zaroz-ca.pem (or PGSSLROOTCERT)
pgx?sslmode=verify-full&sslrootcert=/path/to/zaroz-ca.pem
JDBC?sslmode=verify-full&sslrootcert=/path/to/zaroz-ca.pem

The certificate is the same for every Zaroz database and does not change when you restore a backup.

Complete, ready-to-run code for every client, with each library's exact TLS settings: Connect to PostgreSQL.

When it does not connect

The error saysWhat it meansFix
ENOENT ... zaroz-ca.pemThe URL names a certificate file your app does not haveInside Zaroz, remove sslrootcert from the URL. Outside, use the full path to the downloaded file
SELF_SIGNED_CERT_IN_CHAIN, certificate verify failed, unable to get local issuer certificateYour client does not trust the Zaroz certificateInside Zaroz, restart the app so it picks up /etc/zaroz/ca. Outside, follow the table above
hostname mismatch, does not match host nameYou connect to an address the certificate does not coverUse the address the Connect tab shows
no pg_hba.conf entry ... no encryptionYour client tried without TLSAdd sslmode=verify-full (require for Prisma)

Never use sslmode=no-verify or rejectUnauthorized: false as a fix. It turns off the server check, which is the part that protects you.

Last modified on