job "postgres-server" {
  datacenters = ["dc1"]
  type = "service"
  priority = 80

  group "postgres-server" {
    count = 1

    restart {
      attempts = 10
      interval = "5m"
      delay = "25s"
      mode = "delay"
    }

    network {
      mode = "bridge"

      port "db" {
        to = 5432
        host_network = "wesher"
      }
    }

    volume "postgres-data" {
      type = "host"
      read_only = false
      source = "mysql-data"
    }

    service {
      name = "postgres-server"
      provider = "nomad"
      port = "db"
    }

    task "postgres-server" {
      driver = "docker"

      config {
        image = "postgres:14"
        ports = ["db"]
      }

      volume_mount {
        volume = "postgres-data"
        destination = "/var/lib/postgresql"
        read_only = false
      }

      env = {
        # Allow connections from any host
        "MYSQL_ROOT_HOST" = "%"
      }

      template {
        data = <<EOH
{{ with nomadVar "nomad/jobs/postgres-server" }}
POSTGRES_USER={{ .superuser }}
POSTGRES_PASSWORD={{ .superuser_pass }}
{{ end }}
        EOH
        destination = "secrets/db.env"
        env = true
      }

      resources {
        cpu = 300
        memory = 256
      }
    }
  }
}