Nodevisor Docs
Services

Postgres

PostgreSQL database service with persistent storage and health checks.

Overview

The Postgres service provides a production-ready PostgreSQL database with persistent storage, configurable credentials, and built-in health checks. It extends PortDockerService and exposes a single port.

Quick Start

import { DockerCluster, Postgres } from '@nodevisor/docker';

const cluster = new DockerCluster({ name: 'production', ... });

cluster.addDependency(new Postgres({
  database: 'myapp',
  username: 'myapp',
  password: process.env.DB_PASSWORD!,
}));

Configuration

PostgresConfig

OptionTypeDefaultDescription
namestring'postgres'Service name
versionstring'17.5'PostgreSQL version
imagestring'postgres:{version}'Docker image
portnumber5432PostgreSQL port
databasestringundefinedDatabase to create on first run
usernamestringundefinedDatabase user to create on first run
passwordstringundefinedPassword for the database user
volumeDockerVolumeundefinedCustom volume configuration
healthcheckDockerHealthcheckConfigBuilt-in (see below)Custom health check config

Health Check

PropertyValue
Commandpg_isready -U postgres
Interval10s
Timeout5s
Retries5
Start period15s

Volumes

VolumeTargetTypeDescription
data/var/lib/postgresql/datavolumePersistent database storage

Data persists across container restarts and redeployments.

Environment Variables

The following environment variables are set in the container based on your configuration:

VariableSourceDescription
POSTGRES_PASSWORDpasswordDatabase user password
POSTGRES_USERusernameDatabase user name
POSTGRES_DBdatabaseDatabase to create
POSTGRES_PORTportPostgreSQL port

Usage with DockerCluster

import {
  DockerCluster, DockerNode, ClusterUser,
  Traefik, Postgres, NodeWeb, DockerRegistry,
} from '@nodevisor/docker';

const cluster = new DockerCluster({
  name: 'production',
  nodes: [new DockerNode({ host: '10.0.0.1' })],
  users: [new ClusterUser({ username: 'root', privateKeyPath: '~/.ssh/id_ed25519' })],
  registry: new DockerRegistry({
    server: 'ghcr.io',
    username: 'myorg',
    password: process.env.REGISTRY_TOKEN!,
  }),
});

cluster.addDependency(new Traefik({
  ssl: { email: 'ops@example.com', redirect: true },
}));

cluster.addDependency(new Postgres({
  database: 'myapp',
  username: 'myapp',
  password: process.env.DB_PASSWORD!,
}));

cluster.addDependency(new NodeWeb({
  name: 'api',
  appDir: './apps/api',
  domains: ['api.example.com'],
  environment: {
    DATABASE_URL: `postgresql://myapp:${process.env.DB_PASSWORD}@postgres:5432/myapp`,
  },
}));

await cluster.deploy();

The connection string uses the service name postgres as the hostname — Docker's internal DNS resolves it automatically.

On this page