Deployment not getting marked as healthy for postgresql docker container in Nomad

I am trying to bring up postgres container in Nomad. The problem I am facing is the postgres job deployment is notting marked as healthy, eventhough i can connect to postgres DB from my host machine.

This is my nomad job spec. I feel i am missing something and i couldn’t find any proper answers from any other forums.

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


  group "postgres-server" {
    count = 1

    volume "postgres" {
      type      = "host"
      read_only = false
      source    = "postgres"
    }

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

    network {
      port "db" {
        static = 5432
      }
      port "http" {
          to = 8080
      }
    }

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

      config {
        image = "postgres:9.6"
        ports = ["db", "http"]
      }
      env {
          POSTGRES_USER = "postgres"
          POSTGRES_PASSWORD = "postgres"
          POSTGRES_DB = "postgres"
      }
      volume_mount {
        volume      = "postgres"
        destination = "/var/lib/postgresql/data"
        read_only   = false
      }

      resources {
        cpu    = 500
        memory = 1024
      }

      service {
        name = "postgres"
        port = "db"

        check {
            name = "postgresql_check"
            type = "tcp"
            interval = "60s"
            timeout = "5s"
        }
      }
    }
    
  }
}

Hi @sanjeevkumarraob, can you try moving the service definition to the group level? task level service definitions are effectively deprecated.

e.g.

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


  group "postgres-server" {
    count = 1

    volume "postgres" {
      type      = "host"
      read_only = false
      source    = "postgres"
    }

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

    network {
      port "db" {
        static = 5432
      }
      port "http" {
        to = 8080
      }
    }

    service {
      name = "postgres"
      port = "db"

      check {
        name     = "postgresql_check"
        type     = "tcp"
        interval = "60s"
        timeout  = "5s"
      }
    }

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

      config {
        image = "postgres:9.6"
        ports = ["db", "http"]
      }
      env {
        POSTGRES_USER     = "postgres"
        POSTGRES_PASSWORD = "postgres"
        POSTGRES_DB       = "postgres"
      }
      volume_mount {
        volume      = "postgres"
        destination = "/var/lib/postgresql/data"
        read_only   = false
      }

      resources {
        cpu    = 500
        memory = 1024
      }
    }
  }
}

This can be done using the command type check as follows within service block (let it be inside task):

check {
  name     = "Check Postgres is ready"
  type     = "script"
  command  = "/bin/bash"
  args     = ["-c", "pg_isready"]
  interval = "300s"
  timeout  = "2s"
}

Though if you are using a database in prod env its better to leave this check, comment it out, and have some other alert mechanism for checking if database is down!