<p>using a standard istio deployment in a kubernetes cluster I am trying to add an initContainer to my pod deployment, which does additional database setup.</p>

<p>Using the cluster IP of the database doesn't work either. But I can connect to the database from my computer using port-forwarding.</p>

<p>This container is fairly simple:</p>

<pre><code>    spec:
      initContainers:
        - name: create-database
          image: tmaier/postgresql-client
          args:
            - sh
            - -c
            - |
              psql "postgresql://$DB_USER:$DB_PASSWORD@db-host:5432" -c "CREATE DATABASE fusionauth ENCODING 'UTF-8' LC_CTYPE 'en_US.UTF-8' LC_COLLATE 'en_US.UTF-8' TEMPLATE template0"
              psql "postgresql://$DB_USER:$DB_PASSWORD@db-host:5432" -c "CREATE ROLE user WITH LOGIN PASSWORD 'password';"
              psql "postgresql://$DB_USER:$DB_PASSWORD@db-host:5432" -c "GRANT ALL PRIVILEGES ON DATABASE fusionauth TO user; ALTER DATABASE fusionauth OWNER TO user;"
</code></pre>

<p>This kubernetes initContainer according to what I can see runs before the "istio-init" container. Is that the reason why it cannot resolve the db-host:5432 to the ip of the pod running the postgres service?</p>

<p>The error message in the init-container is:</p>

<pre><code>psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
</code></pre>

<p>The same command from fully initialized pod works just fine.</p>

<p>You can't access services inside the mesh without the Envoy sidecar, your init container runs alone with no sidecars. In order to reach the DB service from an init container you need to expose the DB with a ClusterIP service that has a different name to the Istio Virtual Service of that DB. </p>

<p>You could create a service named <code>db-direct</code> like:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: db-direct
  labels:
    app: db
spec:
  type: ClusterIP
  selector:
    app: db
  ports:
    - name: db
      port: 5432
      protocol: TCP
      targetPort: 5432
</code></pre>

<p>And in your init container use <code>db-direct:5432</code>.</p>
