apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres-statefulset
  namespace: default
  labels:
    app: postgres
spec:
  serviceName: "postgres"
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:12
        env:
          - name: POSTGRES_DB
            valueFrom:
              secretKeyRef:
                name: postgres
                key: db
          - name: POSTGRES_USER
            valueFrom:
              secretKeyRef:
                name: postgres
                key: user
          - name: POSTGRES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: postgres
                key: password
        ports:
        - containerPort: 5432
          name: postgresdb
        volumeMounts:
        - name: data
          mountPath: /data
          subPath: postgres
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: postgres-pvc