<p>I am really struggling regarding how my application which is deployed in <code>--dev namespace</code> can connect to <strong>postgreSQL</strong> database which I deployed independently using helm with <code>--database namespace</code>. What I did so far is as below.</p>

<p>Database and myapp deployed different namespace. I just copy the name <code>PGHOST,PGPASSWORD</code> from some examples but I am not sure where should I use this name and is that has to be same somewhere in postgreSQL?</p>

<p>Should I take care anything else to connect database or is there anything that is not best practice? Should I add a namespace to jdbc url?</p>

<p>Locally we connect to database using below parameters but what should be the way after we deploy our application via helm? We are using sequelize as a client library</p>

<pre><code>const connectionString = postgres://${global.config.database_username}:${global.config.database_password}@${global.config.database_host}:${global.config.database_port}/${global.config.database_name};
</code></pre>

<h2>postgres values</h2>

<pre><code>## Specify PGDATABASE
##
DBName: db
</code></pre>

<h2>After I deployed postgres;</h2>

<pre><code>  # of replicas: 3
  service name:  my-postgres-postgresql-helm
  service port:  64000
  database name: db
  database user: admin
  jdbc url:      jdbc:postgresql://my-postgres-postgresql-helm:port
</code></pre>

<h2>deployment.yaml</h2>

<pre><code>        - name: PGHOST
          valueFrom:
            configMapKeyRef:
              name: {{ .Release.Name }}-configmap
              key: jdbc-url
        - name: PGDATABASE
          value: {{ .Values.postgres.database name | quote }}
        - name: PGPASSWORD
          value: "64000"
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "my-mp.name" . }}
              key: POSTGRES_PASSWORD
</code></pre>

<h2>configmaps.yaml</h2>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ .Release.Name }}-configmap
    labels:
        app.kubernetes.io/name: {{ include "my-mp.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        helm.sh/chart: {{ include "my-mp.chart" . }}
data:
  jdbc-url: jdbc:postgresql://my-postgres-postgresql-helm..
</code></pre>

<h2>values.yaml</h2>

<pre><code>postgres:
  service name:  my-postgres-postgresql-helm
  service port:  64000
  database name: db
  database user: admin
</code></pre>

<p>Is this a typo in your question about the jdbc url jdbc url:      <code>jdbc:postgresql://my-postgre</code>? You have mentioned that the service name is <code>my-postgres-postgresql-helm</code> and hence the jdbc url should be something like: <code>jdbc:postgresql://my-postgres-postgresql-helm.database</code>. Note the <code>.database</code> appended to the service name! Since your application pod is running in a different namespace, you should append the namespace name at the end of the service name. Had they been in the same namespace, you wouldn't need it.</p>

<p>Now, if that doesn't fix it, to debug the issues, this is what I would do if I were you:</p>

<ol>
<li>Check if there any NetworkPolicies which add restrictions on the namespace level; that is allowing traffic only between specific namespaces or even pods, which may prevent the traffic from your application pod reaching your postgres pod.</li>
<li>Make sure your Service for postgres pod is proper. That is, describing the service should list the Pod's IP as Endpoints. If not check the Service's label selector and make sure it uses the same labels as the postgres pod.</li>
<li>Exec into your pod and check if your application pod is able to reach the service through nslookup using the service name, that is <code>my-postgres-postgresql-helm.database</code>.</li>
</ol>

<p>If all these tests are positive and working, then most probably it is some other configuration issue. Let me know if this fixes your issue and GL.</p>
