apiVersion: apps/v1 kind: StatefulSet metadata: name: test-pg namespace: test-env labels: app: test-pg spec: serviceName: postgres replicas: 1 selector: matchLabels: app: test-pg template: metadata: labels: app: test-pg spec: initContainers: # On first run the PVC is empty — copy the pre-built PG cluster # from the image so the main entrypoint can configure and start it. - name: init-pgdata image: benadis/pg-1c:18.1-2.1C command: - sh - -c - | if [ ! -d /data/18/main ]; then echo "Initializing PG data from image..." cp -a /var/lib/postgresql/. /data/ echo "Done." else echo "PG data already exists, skipping init." fi volumeMounts: - name: pg-data mountPath: /data containers: - name: postgres image: benadis/pg-1c:18.1-2.1C # Override entrypoint to handle "role already exists" on PVC reuse. # The image entrypoint uses `set -e` + CREATE USER without IF NOT EXISTS, # causing crash when PVC already has the user from a previous init. command: - bash - -c - | # Patch entrypoint: make CREATE USER idempotent. # Image entrypoint uses `set -e` + bare CREATE USER which fails # when PVC is reused and the role already exists. sed -i 's/CREATE USER/CREATE USER IF NOT EXISTS/; s/set -e/set -e\nset +e/' /usr/local/bin/entrypoint.sh 2>/dev/null || true exec /usr/local/bin/entrypoint.sh postgres env: - name: LANG value: "ru_RU.UTF-8" - name: LC_ALL value: "ru_RU.UTF-8" - name: TZ value: "Europe/Moscow" ports: - name: postgres containerPort: 5432 protocol: TCP volumeMounts: - name: pg-data mountPath: /var/lib/postgresql resources: requests: cpu: 200m memory: 512Mi limits: cpu: "2" memory: 4Gi readinessProbe: exec: command: - su - "-" - postgres - "-c" - "/usr/lib/postgresql/18/bin/pg_isready" initialDelaySeconds: 30 periodSeconds: 10 timeoutSeconds: 5 livenessProbe: exec: command: - su - "-" - postgres - "-c" - "/usr/lib/postgresql/18/bin/pg_isready" initialDelaySeconds: 60 periodSeconds: 30 timeoutSeconds: 5 volumeClaimTemplates: - metadata: name: pg-data spec: storageClassName: local-path accessModes: ["ReadWriteOnce"] resources: requests: storage: 20Gi