title: Running rails + postgres in kubernetes: Connection refused
body: <p>My rails deployment is not able to connect to the postgres database. Error in the logs:</p>
  
  <pre><code>PG::ConnectionBad (could not connect to server: Connection refused
    Is the server running on host "db" (10.0.105.11) and accepting
    TCP/IP connections on port 5432?
  ):
  </code></pre>
  
  <p>This is my <code>kubectl get services</code> output:</p>
  
  <pre><code>NAME                     TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
  load-balancer            LoadBalancer   10.0.5.147     60.86.4.33    80:31259/TCP   10m
  db                       ClusterIP      10.0.105.11    &lt;none&gt;        5432/TCP       10m
  kubernetes               ClusterIP      10.0.0.1       &lt;none&gt;        443/TCP        10m
  web                      ClusterIP      10.0.204.107   &lt;none&gt;        3000/TCP       10m
  </code></pre>
  
  <p>db-deployment.yaml:</p>
  
  <pre><code>apiVersion: apps/v1
  kind: Deployment
  metadata:
    labels:
      app: db
    name: db
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: db
    strategy:
      type: Recreate
    template:
      metadata:
        labels:
          app: db
      spec:
        containers:
        - env:
          - name: POSTGRES_DB
            value: postgres
          - name: POSTGRES_HOST_AUTH_METHOD
            value: trust
          - name: POSTGRES_USER
            value: postgres
          - name: PGDATA
            value: /var/lib/postgresql/data/pgdata
          ports:
          - containerPort: 5432
          image: postgres
          imagePullPolicy: ""
          name: db
          resources: {}
          volumeMounts:
          - mountPath: /var/lib/postgresql/data
            name: postgres
        restartPolicy: Always
        serviceAccountName: ""
        volumes:
        - name: postgres
          persistentVolumeClaim:
            claimName: postgres
  status: {}
  </code></pre>
  
  <p>db-service.yaml:</p>
  
  <pre><code>apiVersion: v1
  kind: Service
  metadata:
    name: db
    labels:
      app: db
  spec:
    ports:
      - port: 5432
    selector:
      app: db
      tier: database
  </code></pre>
  
  <p>config/database.yml:</p>
  
  <pre><code>default: &amp;default
    adapter: postgresql
    encoding: utf8
    host: db
    username: postgres
    pool: 5
  
  development:
    &lt;&lt;: *default
    database: myapp_development
  
  test:
    &lt;&lt;: *default
    database: myapp_test
  
  production:
    &lt;&lt;: *default
    database: myapp_production
  </code></pre>
  
  <p>How do I ensure the postgres instance is running and accepting connections? Did I misconfigure something in the manifest files, or does the database.yml file need to point to a different host?</p>