<p>I follow <a href="https://www.keycloak.org/docs/latest/server_installation/#_external_database" rel="nofollow noreferrer">this</a> but it is not working.</p>
<p>I created a custom secret:</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
    name: keycloak-db-secret
data:
    POSTGRES_DATABASE: ...
    POSTGRES_EXTERNAL_ADDRESS: ...
    POSTGRES_EXTERNAL_PORT: ...
    POSTGRES_HOST: ...
    POSTGRES_USERNAME: ...
    POSTGRES_PASSWORD: ...
</code></pre>
<p>and keycloak with external db:</p>
<pre><code>apiVersion: keycloak.org/v1alpha1
kind: Keycloak
metadata:
  labels:
      app: keycloak
  name: keycloak
spec:
  externalDatabase:
    enabled: true
  instances: 1
</code></pre>
<p>but when I check log, keycloak can not connect to db. It is still using default vaule: <code>keycloak-postgresql.keycloak</code> not value defined in my custom secret ? Why it is not using my value from secrets ?</p>
<p><strong>UPDATE</strong></p>
<p>when I check keycloak pod which was created by operator I can see:</p>
<pre><code>  env:
    - name: DB_VENDOR
      value: POSTGRES
    - name: DB_SCHEMA
      value: public
    - name: DB_ADDR
      value: keycloak-postgresql.keycloak
    - name: DB_PORT
      value: '5432'
    - name: DB_DATABASE
      value: keycloak
    - name: DB_USER
      valueFrom:
        secretKeyRef:
          name: keycloak-db-secret
          key: POSTGRES_USERNAME
    - name: DB_PASSWORD
      valueFrom:
        secretKeyRef:
          name: keycloak-db-secret
          key: POSTGRES_PASSWORD
</code></pre>
<p>so now I know why I can not connect to db. It use different DB_ADDR. How I can use address: my-app.postgres (db in another namespace).</p>
<p>I dont know why POSTGRES_HOST in secret not working and pod still using default service name</p>

hudi
<p>To connect with service in another namespace you can use.</p>
<pre><code>&lt;servicename&gt;.&lt;namespace&gt;.svc.cluster.local
</code></pre>
<p>suppose your Postgres deployment and service running in <strong>test</strong> namespace it will go like</p>
<pre><code>postgres.test.svc.cluster.local
</code></pre>
<p>this is what i am using : <a href="https://github.com/harsh4870/Keycloack-postgres-kubernetes-deployment/blob/main/keycload-deployment.yaml" rel="nofollow noreferrer">https://github.com/harsh4870/Keycloack-postgres-kubernetes-deployment/blob/main/keycload-deployment.yaml</a></p>
<p>i have also attached the Postgres file you can use it however in my case i have setup both in the <strong>same</strong> namespace <strong>keycloak</strong> and <strong>Postgres</strong> so working like charm.</p>
