<p>I have kubernetes cluster with two replicas of a PostgreSQL database in it, and I wanted to see the values stored in the database.</p>
<p>When I <code>exec</code> myself into one of the two postgres pod (<code>kubectl exec --stdin --tty [postgres_pod] -- /bin/bash</code>) and check the database from within, I have only a partial part of the DB. The rest of the DB data is on the other Postgres pod, and I don't see any directory created by the persistent volumes with all the database stored.</p>
<p>So in short I create 4 tables; in one <em>postgres pod</em> I have 4 tables but 2 are empty, in the other <em>postgres pod</em> there are 3 tables and the tables that were empty in the first pod, here are filled with data.</p>
<p>Why the pods don't have the same data in it?</p>
<p>How can I access and download the entire database?</p>
<p>PS. I deploy the cluster using HELM in minikube.</p>
<hr />
<p>Here are the YAML files:</p>
<pre><code>---
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  POSTGRES_DB: database-pg
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: postgres
  PGDATA: /data/pgdata
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    type: local
    app: postgres
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: &quot;/mnt/data&quot;
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  ports:
    - name: postgres
      port: 5432
      nodePort: 30432
  type: NodePort
  selector:
    app: postgres
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
spec:
  serviceName: postgres-service
  selector:
    matchLabels:
      app: postgres
  replicas: 2
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:13.2
          volumeMounts:
            - name: postgres-disk
              mountPath: /data
          # Config from ConfigMap
          envFrom:
            - configMapRef:
                name: postgres-config
  volumeClaimTemplates:
    - metadata:
        name: postgres-disk
      spec:
        accessModes: [&quot;ReadWriteOnce&quot;]
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  replicas: 2
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:13.2
          imagePullPolicy: IfNotPresent
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim
---


</code></pre>

janboro
<p>I found a solution to my problem of downloading the volume directory, however when I run multiple replicasets of postgres, the tables of the DB are still scattered between the pods.</p>
<p>Here's what I did to download the postgres volume:</p>
<p>First of all, minikube supports some specific directories for volume appear:</p>
<blockquote>
<p>minikube is configured to persist files stored under the following directories, which are made in the Minikube VM (or on your localhost if running on bare metal). You may lose data from other directories on reboots.</p>
<pre><code>/data
/var/lib/minikube
/var/lib/docker
/tmp/hostpath_pv
/tmp/hostpath-provisioner
</code></pre>
</blockquote>
<p>So I've changed the mount path to be under the <code>/data</code> directory. This made the database volume visible.</p>
<p>After this I ssh'ed into minikube and copied the database volume to a new directory (I used <code>/home/docker</code> as the user of <code>minikube</code> is <code>docker</code>).</p>
<pre><code>sudo cp -R /data/pgdata /home/docker
</code></pre>
<p>The volume <code>pgdata</code> was still owned by <code>root</code> (access denied error) so I changed it to be owned by <code>docker</code>. For this I also set a new password which I knew:</p>
<pre><code>sudo passwd docker # change password for docker user
sudo chown -R docker: /home/docker/pgdata # change owner from root to docker
</code></pre>
<p>Then you can exit and copy the directory into you local machine:</p>
<pre><code>exit
scp -r $(minikube ssh-key) docker@$(minikube ip):/home/docker/pgdata [your_local_path].
</code></pre>
<p><em>NOTE</em></p>
<p>Mario's advice, which is to use <code>pgdump</code> is probably a better solution to copy a database. I still wanted to download the volume directory to see if it has the full database, when the pods have only a part of all the tables. In the end it turned out it doesn't.</p>
