apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
  namespace: trekr
spec:
  accessModes: [ReadWriteOnce]
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
  namespace: trekr
spec:
  serviceName: postgres
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:16-alpine
          env:
            - name: POSTGRES_DB
              value: trekr
            - name: POSTGRES_USER
              value: trekr
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: trekr-secrets
                  key: db-password
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
            - name: init-sql
              mountPath: /docker-entrypoint-initdb.d
          readinessProbe:
            exec:
              command: [pg_isready, -U, trekr, -d, trekr]
            initialDelaySeconds: 5
            periodSeconds: 5
            failureThreshold: 10
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: postgres-pvc
        - name: init-sql
          configMap:
            name: trekr-db-init
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: trekr
spec:
  selector:
    app: postgres
  ports:
    - port: 5432
      targetPort: 5432
