Skip to content

PostgreSQL connection from applications

Connect Java, PHP, Python, and Node.js applications to PostgreSQL on Stack Harbor PaaS — JDBC, libpq, psycopg, pg. SSL-secured connections and the host options.

This page covers the application-side connection details for PostgreSQL on Stack Harbor PaaS. For the host options (CNAME, private IP, public IP) see Database connection strings first; this page focuses on the per-language driver code and the SSL setup.

Java (JDBC)

Use the official PostgreSQL JDBC driver:

String URL = "jdbc:postgresql://node{node_id}-{environment_name}.{domain}/{dbname}";
Connection conn = DriverManager.getConnection(URL, user, password);

Use a connection pool (HikariCP, c3p0) in production rather than opening a fresh connection per query. PostgreSQL connections are heavy — each opens a backend process — so pooling matters more here than on MySQL.

For UTF-8 encoding, PostgreSQL stores text in the database’s encoding (typically UTF-8 by default); no driver flag is usually needed.

PHP

Using libpq:

$conn = pg_connect("host=node{node_id}-{environment_name}.{domain} port=5432 dbname=postgres user=webadmin password={password}");

Or via PDO:

$pdo = new PDO(
    'pgsql:host=node{node_id}-{environment_name}.{domain};dbname={dbname}',
    $user,
    $password
);

The host string must not include http:// — the database is not an HTTP endpoint.

Python (psycopg)

psycopg3 is the current driver; psycopg2 works the same way at the connect() call:

import psycopg

conn = psycopg.connect(
    host='node{node_id}-{environment_name}.{domain}',
    user=user,
    password=password,
    dbname=dbname,
)

For an asyncio application:

import psycopg

async with await psycopg.AsyncConnection.connect(
    host='node{node_id}-{environment_name}.{domain}',
    user=user,
    password=password,
    dbname=dbname,
) as conn:
    ...

Node.js (pg)

The standard pg driver:

import pg from 'pg';

const client = new pg.Client({
  host: 'node{node_id}-{environment_name}.{domain}',
  user,
  password,
  database: dbname,
  port: 5432,
});

await client.connect();

For pooled connections, use pg.Pool — the pool reuses connections and is the right default for any web application.

SSL-secured connections

For environments where TLS between the application and the database is required, install the Encryption in Transit add-on on the database node. The add-on issues a server certificate and turns on TLS in postgresql.conf.

On the application side:

  • Java — append ?ssl=true&sslmode=require (or verify-full for cert validation) to the JDBC URL.
  • PHP / libpq — add sslmode=require to the connection string.
  • Python (psycopg) — pass sslmode='require' to connect().
  • Node.js (pg) — pass ssl: { rejectUnauthorized: true, ca: <cert> } to the client constructor.

The sslmode levels — disable, allow, prefer, require, verify-ca, verify-full — are the same ones libpq exposes. verify-full is the strictest; require accepts any server certificate without verification.

Notes

  • No localhost. Application and database are on separate containers. Use the database node’s CNAME, private IP, or public IP.
  • Default port. PostgreSQL listens on 5432 by default. The drivers above assume that port unless overridden.
  • Initial credentials. The platform emails the initial superuser and password when the container is created. Create application-scope users at lesser privilege from pgAdmin or via CREATE ROLE.

See also