# Unable to connect db with api service using nomad as provider

**URL:** <https://discuss.hashicorp.com/t/unable-to-connect-db-with-api-service-using-nomad-as-provider/48312>\
**Category:** Nomad\
**Created:** [December 25, 2022, 9:19am UTC](https://discuss.hashicorp.com/t/unable-to-connect-db-with-api-service-using-nomad-as-provider/48312 "2022-12-25T09:19:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![JitenPalaparthi](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jitenpalaparthi/32/32781_2.png) [@JitenPalaparthi](https://discuss.hashicorp.com/u/JitenPalaparthi)\
**Post date:** [December 25, 2022, 9:19am UTC](https://discuss.hashicorp.com/t/unable-to-connect-db-with-api-service-using-nomad-as-provider/48312/1 "2022-12-25T09:19:38Z")

</div>

my app is not conecting to database; I am using simple nomad service discovery. Here is my job file any help please

variable “datacenters” {  
description = “A list of datacenters in the region which are eligible for task placement.”  
type = list(string)  
default = [“dc1”]  
}

variable “region” {  
description = “The region where the job should be placed.”  
type = string  
default = “global”  
}

variable “postgres\_db” {  
description = “Postgres DB name”  
default = “vehicle\_master\_bd”  
}

variable “postgres\_user” {  
description = “Postgres DB User”  
default = “postgres”  
}

variable “postgres\_password” {  
description = “Postgres DB Password”  
default = “postgres”  
}

# Begin Job Spec

job “cres-vehicleMaster” {  
type = “service”  
region = var.region  
datacenters = var.datacenters  
group “db” {  
network {  
port “db” {  
to = 5432  
}  
}

```
task "postgres" {
  driver = "docker"

  meta {
    service = "database"
  }
  service {
    name = "database"
    port = "db"
    provider = "nomad"
  }
  config {
    image = "postgres"
    ports = ["db"]
  }
  resources {
    cpu = 500
    memory = 500
  }
  env {

    POSTGRES_DB = var.postgres_db
    POSTGRES_USER = var.postgres_user
    POSTGRES_PASSWORD = var.postgres_password
  }
}

```

}  
group “vehicleMaster-api” {  
count = 1  
network {  
port “api” {  
to = 50080  
}  
}

```
restart {
  attempts = 2
  interval = "30m"
  delay = "15s"
  mode = "fail"
}

task "vehicleMaster-api" {
  driver = "docker"
  service {
    name = "vehicleMaster-api"
    tags = ["vehicleMaster", "RESTAPI"]
    port = "api"
    provider = "nomad"
  }
  template {
    data = <<EOH

```

{{ range nomadService “database” }}  
DB\_CONN=“host={{ .Address }} port={{ .Port }} user={var.postgres\_user} password={var.postgres\_password} dbname={var.postgres\_db} sslmode=disable" {{ end }} EOH destination = "local/env.txt" env = true } env { // DB\_CONN = "host=127.0.0.1 port=5432 user=postgres password=postgres dbname=vehicle\_master\_bd sslmode=disable" // DB\_CONN = "host={NOMAD\_IP\_postgres} port={NOMAD\_PORT\_postgres} user={var.postgres\_user} password={var.postgres\_password} dbname={var.postgres\_db} sslmode=disable”  
PORT = “50080”  
}  
config {  
image = “jpalaparthi/vehiclemaster:v0.0.3”  
ports = [“api”]  
}  
resources {  
cpu = 500 # 500 MHz  
memory = 512 # 256MB  
}  
}  
}  
}
