Why is Postgres data deleted when pod restarted on Kubernetes?
Linode
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:
- kubectl apply -f postgres-secrets.yaml
- kubectl create ns postgres
- kubectl -n postgres apply -f postgres-pv-claim.yaml
- kubectl -n postgres apply -f postgres.yaml
- Enter pod: kubectl exec -it postgres-0 bash
- Use psql: psql --username=xyzapi init-db
- 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
- Exit pod
- Delete pod: kubectl -n postgres delete pod postgres-0
- When pod is recreated, enter it again: kubectl exec -it postgres-0 bash
- Use psql again: psql --username=xyzapi init-db
- Check tables: /dt
- Issue - no table there, expected it to be there.
1 Reply
jcardillo
Linode Staff
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: