DevPanel
Guides
Intermediate8 min read

Deploy PostgreSQL

Run a persistent PostgreSQL instance on your server and connect it to your application.

Step 1: Create the service

compose.yml
services:
  db:
    image: postgres:16-alpine
    restart: unless-stopped
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

Add POSTGRES_USER, POSTGRES_PASSWORD, and POSTGRES_DB in the environment variables panel.

Step 2: Connect your app

env
DATABASE_URL=postgresql://myapp:password@<server-ip>:5432/myapp_prod

Add this as an environment variable in your application's DevPanel service.

Step 3: Restrict access (recommended)

Place both services in the same Compose file so they share a private Docker network — no port 5432 exposure needed.

compose.yml
services:
  app:
    image: ghcr.io/youruser/myapp:latest
    environment:
      DATABASE_URL: postgresql://myapp:${POSTGRES_PASSWORD}@db:5432/myapp_prod
    depends_on: [db]
    restart: unless-stopped

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: myapp
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: myapp_prod
    volumes:
      - pgdata:/var/lib/postgresql/data
    restart: unless-stopped

volumes:
  pgdata:

With this setup, only the app service can reach the database. Port 5432 is never exposed to the internet.