apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: postgres
    spec:
      serviceAccountName: postgresql
      containers:
        - image: postgres:14.7-alpine3.17
          name: postgres
          env:
            - name: POSTGRES_DB
              value: postgres
            - name: POSTGRES_USER
              value: user
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-secret
                  key: root_password
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          ports:
            - containerPort: 5432
              name: postgres
          readinessProbe:
            exec:
              command: ["psql", "-U", "user", "-d", "postgres", "-c", "SELECT 1"]
            initialDelaySeconds: 15
            timeoutSeconds: 2
          volumeMounts:
            - name: postgres-stateful-data
              mountPath: /var/lib/postgresql/data
      volumes:
        - name: postgres-stateful-data
          persistentVolumeClaim:
            claimName: postgres-pvc
