Skip to content

PostgreSQL Docker Setup ​

Prerequisites ​

  • Docker must be installed and running.
  • Docker Compose (usually included with Docker Desktop)

1) Use Docker Compose ​

Create a docker-compose.yml:

yaml
services:
  db:
    image: postgres:16
    container_name: pg
    restart: unless-stopped
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydb
    ports:
      - "5432:5432"
    volumes:
      - pg-data:/var/lib/postgresql/data
volumes:
  pg-data:

Run it:

bash
docker compose up -d

Stop it:

bash
docker compose down

Note: down doesn’t delete the volume. To remove data too: docker compose down -v.

2) Connection String (with example values) ​

postgres://user:password@localhost:5432/mydb