<p>I have a kubernetes cluster running an app. Part of the cluster is a postgresql pod, currently running version 10.4. Unfortunately, I discovered that I need to upgrade the postgresql version.</p>
<p>The postgres yaml is as follow:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:10.4
          imagePullPolicy: &quot;IfNotPresent&quot;
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim

</code></pre>
<p>The postgresql database already has some data in it. I need to find a way to upgrade the cluster while in production.</p>
<p>If I simply try to change the image to 12.0 and run <code>kubectl apply</code> I get an error:</p>
<pre><code>2020-11-15 22:48:08.332 UTC [1] DETAIL:  The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 12.5 (Debian 12.5-1.pgdg100+1).
</code></pre>
<p>So I understand that I need to manually upgrade the postgres database inside the cluster, and only then I will be able to fix the yaml. Is that correct?</p>

justadev
<p>I tried @Justin method, but I encountered an issue that I couldn't stop current running postgres process inside the pod (for some reason inside the container there is no access to postgresql service. You can see more about that issue <a href="https://stackoverflow.com/questions/64855485/how-do-i-stop-postgresql-server-inside-container/64857097#64857097">here</a>)</p>
<p>Since I couldn't upgrade the postgresql specifically inside the pod, what I did at the end is creating a parallel postgres pod in Kubernetes which holds the new version. Then I dumped database from old server, copied it to the new server, and used it to initialize the database there.</p>
<p>Here are the steps one by one:</p>
<ol>
<li><p>Create a parallel postgres service with the new version</p>
</li>
<li><p>In old version pod:</p>
</li>
</ol>
<pre><code>pg_dumpall -U postgresadmin -h localhost -p 5432 &gt; dumpall.sql
</code></pre>
<ol start="3">
<li>In the host:</li>
</ol>
<pre><code>kubectl cp postgres-old-pod:/dumpall.sql dumpall.sql
kubectl cp dumpall.sql postgres2-new-pod:/dumpall.sql
</code></pre>
<ol start="4">
<li><p>ssh to new-pod</p>
</li>
<li><p>extra step that I needed, becuase for some reason new pod didn't had 'postgres' user created:
get into postgres client using your credentials:</p>
</li>
</ol>
<pre><code>psql postgresql://postgresadmin:pass1234@127.0.0.1:5432/postgresdb?sslmode=disable
postgresdb=# CREATE ROLE postgres LOGIN SUPERUSER PASSWORD 'somepassword123';
</code></pre>
<p>then exit postgres and exit to normal user</p>
<ol start="6">
<li>Finally update the database:</li>
</ol>
<pre><code>psql -U postgres -W -f dumpall.sql
</code></pre>
