PostgreSQL Docker Setup with Init Script
Docker Compose for PostgreSQL with volume persistence, init scripts, and connection pooling.
# docker-compose.postgres.yml
version: '3.8'
services:
postgres:
image: postgres:16-alpine
environment:
POSTGRES_USER: appuser
POSTGRES_PASSWORD: apppassword
POSTGRES_DB: appdb
PGDATA: /var/lib/postgresql/data/pgdata
ports:
- '5432:5432'
volumes:
- pgdata:/var/lib/postgresql/data
- ./init:/docker-entrypoint-initdb.d
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U appuser -d appdb']
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
shm_size: 128mb
command:
- postgres
- -c
- shared_buffers=256MB
- -c
- max_connections=200
- -c
- work_mem=4MB
- -c
- log_statement=mod
volumes:
pgdata:
# init/01-extensions.sql
# CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
# CREATE EXTENSION IF NOT EXISTS "pgcrypto";Use Cases
- Local PostgreSQL for development
- Database initialization with seed scripts
- Tuned PostgreSQL for testing workloads
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Docker Compose Multi-Service Setup
Docker Compose configuration for a Node.js app with PostgreSQL, Redis, and Nginx reverse proxy.
Redis Docker Setup with Persistence
Docker Compose for Redis with persistence, password auth, memory limits, and a health check.
Dockerfile Multi-Stage Build
Multi-stage Dockerfile for Node.js with build, prune, and minimal production image layers.
GitHub Actions Docker Build and Push
GitHub Actions workflow to build a Docker image and push it to GitHub Container Registry on release.