Why is Postgres data deleted when pod restarted on Kubernetes?

Linode Staff

Expected: test table should be retained in the PV storage after

postgres pod restart

Actual: data is not stored when recreating pod and there are no errors. Everything seems to be working fine, but it does not behave as expected when I create table -> delete pod -> enter the new pod and check tables list.

Steps to reproduce:

  1. kubectl apply -f postgres-secrets.yaml
  2. kubectl create ns postgres
  3. kubectl -n postgres apply -f postgres-pv-claim.yaml
  4. kubectl -n postgres apply -f postgres.yaml
  5. Enter pod: kubectl exec -it postgres-0 bash
  6. Use psql: psql --username=xyzapi init-db
  7. Create Table :
    CREATE TABLE COMPANY(
    ID INT PRIMARY KEY NOT NULL,
    NAME TEXT NOT NULL,
    AGE INT NOT NULL,
    ADDRESS CHAR(50),
    INCOME REAL
    );

7.1 Ensure table is there: \dt

  1. Exit pod
  2. Delete pod: kubectl -n postgres delete pod postgres-0
  3. When pod is recreated, enter it again: kubectl exec -it postgres-0 bash
  4. Use psql again: psql --username=xyzapi init-db
  5. Check tables: /dt
  6. Issue - no table there, expected it to be there.

1 Reply

This appears to be a PostgreSQL issue and can be resolved by adding the following to you StatefulSet -> Deployment:

env:
  - name: PGDATA
    value: /var/lib/postgresql/data/pgdata

See:

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct