title: Flask (SqlAlchemy) + PostgreSQL in Kubernetes: Connection refused
body: <p>I'm getting the following error when running a Flask + PostgreSQL API:</p>
  <pre><code>sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
  Is the server running on host &quot;my-host&quot; (xx.xx.xx.xx) and accepting
  TCP/IP connections on port 5432?
  </code></pre>
  <p>I run my server with gunicorn:</p>
  <pre><code>gunicorn -b 0.0.0.0:5000 --chdir ./src app:app
  </code></pre>
  <p>And these are my k8s configurations.</p>
  <p>deployment.yaml:</p>
  <pre><code>---
  apiVersion: v1
  kind: PersistentVolumeClaim
  metadata:
    name: api-database
  spec:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 5Gi
    storageClassName: longhorn
  
  ---
  apiVersion: v1
  kind: Service
  metadata:
    name: api-database
    labels:
      app: api-database
  spec:
    selector:
      app: api-database
    ports:
      - port: 5432
        targetPort: 5432
        protocol: TCP
  
  ---
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: api-database
    namespace: my-namespace
    labels:
      app: api-database
  spec:
    replicas: 1
    selector:
      matchLabels:
        name: api-database
    template:
      metadata:
    labels:
      name: api-database
  spec:
    volumes:
      - name: data
        persistentVolumeClaim:
          claimName: api-database
  
    containers:
      - name: api-database
        image: postgres:13-alpine
        ports:
          - containerPort: 5432
            name: postgres
  
        volumeMounts:
          - name: data
            mountPath: /var/lib/postgresql/db-data
  
        resources:
          requests:
            memory: &quot;128Mi&quot;
            cpu: &quot;250m&quot;
          limits:
            memory: &quot;256Mi&quot;
            cpu: &quot;500m&quot;
  
        envFrom:
          - secretRef:
              name: api-properties
  </code></pre>
  <p>api.yaml:</p>
  <pre><code>---
  apiVersion: v1
  kind: Service
  metadata:
    name: api-app
    namespace: my-namespace
    labels:
      app: api-app
  spec:
    selector:
      name: api-app
    ports:
    - protocol: TCP
      port: 80
      targetPort: 5000
  
  ---
  apiVersion: v1
  kind: PersistentVolumeClaim
  metadata:
    name: api-packages
  spec:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 5Gi
    storageClassName: longhorn
  
  ---
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: api-app
    namespace: my-namespace
  
  spec:
    replicas: 1
    selector:
      matchLabels:
    app: api-app
    template:
      metadata:
        labels:
          app: api-app
  spec:
    hostname: api
    subdomain: api-app
  
    containers:
    - name: api-app
      image: my-image:latest
      imagePullPolicy: Always
      ports:
        - containerPort: 5000
      
  
      resources:
        requests:
          memory: &quot;64Mi&quot;
          cpu: &quot;250m&quot;
        limits:
          memory: &quot;128Mi&quot;
          cpu: &quot;500m&quot;
      
      envFrom:
        - secretRef:
            name: api-properties
    
    imagePullSecrets:
      - name: registry-key
  </code></pre>
  <p>ingress.yaml:</p>
  <pre><code>apiVersion: networking.k8s.io/v1
  kind: Ingress
  metadata:
    annotations:
      cert-manager.io/cluster-issuer: letsecrypt
      nginx.ingress.kubernetes.io/proxy-body-size: 10m
      nginx.ingress.kubernetes.io/proxy-read-timeout: &quot;900&quot;
      nginx.ingress.kubernetes.io/proxy-send-timeout: &quot;900&quot;
    name: api
  spec:
    rules:
    - host: my-host.com
      http:
        paths:
        - backend:
            service:
              name: api-app
              port:
                number: 5000
      path: /
      pathType: Prefix
    tls:
    - hosts:
      - my-host.com
      secretName: api-app-cert
  </code></pre>
  <p>My connection URL is the following:</p>
  <pre><code>DATABASE_URL=&quot;postgresql://postgres:postgres@api-database:5432/postgres&quot;
  </code></pre>
  <p>And my database environmental variables are the following:</p>
  <pre><code>POSTGRES_DB=postgres
  POSTGRES_USER=postgres
  POSTGRES_PASSWORD=postgres
  </code></pre>
  <p>Everything works properly when developing locally; however, the connection doesn't work when deployed. The API image works also when run locally. Is there anything wrong with the k8s configurations?</p>