Skip to content

Matches page titles and page text. Forty pages, indexed at build time.

Client examples

Connection examples for psql, Node, Python, Go, Ruby on Rails, Django and JDBC. Every one encrypts and keeps the password out of the URL, with the forms that also verify the certificate.

Connection examples for the command line and for seven drivers. Every one encrypts the connection with sslmode=require, and none of them puts a password in a connection string.

To check the certificate as well, use the forms under verifying the certificate, which are what the Connect tab's Verify the certificate switch gives. There is no file to download for either: your endpoint presents a certificate issued by a public authority.

To use one: replace db-example-pooler.fsn1.baltadb.net with the hostname on your service's Connect tab. Every service is created with the role app_writer and the database appdb. Set PGPASSWORD in the environment your process reads; where an example shows a password, <password> stands in for it.

No example names a port. Both endpoints use 5432, the standard PostgreSQL port, and are told apart by hostname. Every example here is the pooled endpoint; drop -pooler from the name for the direct one.

Command line and configuration

export PGPASSWORD=<password>
psql "postgresql://[email protected]/appdb?sslmode=require"

Drivers

db.ts
// Run: npm install pg && node db.ts
// Node 22.18 and later run a .ts file as it is.
import { Pool } from 'pg';

// node-postgres reads the password from PGPASSWORD. Keep it out of source.
const pool = new Pool({
// uselibpqcompat=true gives sslmode=require its standard meaning: encrypted,
// certificate not checked. Without it node-postgres 8 prints a warning.
connectionString: 'postgresql://[email protected]/appdb?sslmode=require&uselibpqcompat=true',
});

const { rows } = await pool.query('select version()');
console.log(rows[0].version);
await pool.end();

Verifying the certificate

sslmode=require encrypts the connection and does not check who answered. These forms check the certificate chain and that the hostname matches. Each was run against a service through Balta's router and shown to connect, to refuse a certificate from an authority the client does not trust, and to refuse a trusted certificate for another name.

# libpq 16 or newer. An older libpq refuses this line rather than connect unchecked.
psql "postgresql://[email protected]/appdb?sslmode=verify-full&sslrootcert=system"

Ruby's pg gem, and Rails with it, have no verified form here: we found no form that verifies on every system without naming a file on your computer.

Django hands OPTIONS to psycopg, so the Python form works there: 'OPTIONS': {'sslmode': 'verify-full', 'sslrootcert': certifi.where()}.

Set an application name

It turns "an unidentified backend is holding a lock" into "the nightly export is holding a lock":

sql
SELECT application_name FROM pg_stat_activity WHERE pid = pg_backend_pid();

What you will see

A working connection answers \conninfo with your database, your role, the host you chose, and SSL Connection true. The host ending in -pooler is the pooled endpoint, and anything session-scoped in your application belongs on the direct one instead.

Troubleshooting

certificate verify failed with a verified form. The client could not check the certificate against the roots it reads. Use the form above for that client, unedited: psycopg's needs pip install certifi, and the psql form needs libpq 16 or newer.

A Java stack trace through org.postgresql.ssl.LibPQFactory, ending in FileNotFoundException for ~/.postgresql/root.crt. The JDBC URL is missing sslfactory=org.postgresql.ssl.DefaultJavaSSLFactory. pgjdbc looked for a root certificate file rather than asking the JVM.

root certificate file "system" does not exist. The libpq behind your client is older than 16 and read the verified form's system as a file name. The default line works on libpq 14 and newer.

Rails or Django connects and then a background job misbehaves. The job is probably using LISTEN, a session advisory lock or a session-level SET. Give that process the direct endpoint. See pooled vs direct.

Your client did not send the server name. Balta finds your database by the hostname your client sends during TLS. Every driver above sends it. Check for sslsni=0, for a libpq older than 14, and for a host given as an IP address.

PGPASSWORD is ignored. The process did not inherit it. In a container, pass it through the container's own environment rather than exporting it in the shell that launched the build.