Skip to content

PostgreSQL Database directory appears to contain a database; Skipping initialization #196

@joshuacox

Description

@joshuacox

I dont seem to be able to run the primary_init_script.sh:

PostgreSQL Database directory appears to contain a database; Skipping initialization

2025-05-22 03:25:54.709 GMT [1] LOG:  starting PostgreSQL 17.5 on x86_64-pc-linux-musl, compiled by gcc (Alpine 14.2.0) 14.2.0, 64-bit
2025-05-22 03:25:54.709 GMT [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2025-05-22 03:25:54.709 GMT [1] LOG:  listening on IPv6 address "::", port 5432
2025-05-22 03:25:54.718 GMT [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2025-05-22 03:25:54.725 GMT [35] LOG:  database system was interrupted; last known up at 2025-05-22 03:25:54 GMT
2025-05-22 03:25:56.539 GMT [35] LOG:  database system was not properly shut down; automatic recovery in progress
2025-05-22 03:25:56.542 GMT [35] LOG:  redo starts at 0/14EAC68
2025-05-22 03:25:56.542 GMT [35] LOG:  invalid record length at 0/14F3500: expected at least 24, got 0
2025-05-22 03:25:56.542 GMT [35] LOG:  redo done at 0/14F3498 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
2025-05-22 03:25:56.549 GMT [33] LOG:  checkpoint starting: end-of-recovery immediate wait
2025-05-22 03:25:56.596 GMT [33] LOG:  checkpoint complete: wrote 17 buffers (0.1%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.008 s, sync=0.028 s, total=0.051 s; sync files=13, longest=0.003 s, average=0.003 s; distance=34 kB, estimate=34 kB; lsn=0/14F3500, redo lsn=0/14F3500
2025-05-22 03:25:56.601 GMT [1] LOG:  database system is ready to accept connections

but the PVC did not exist prior, so why does it think there is a database directory?

My config:

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-postgres-conf
  namespace: example
data:
  primary_init_script.sh: |
    #!/usr/bin/env bash
    set -e
    echo primary_init_script.sh

    # This script assumes that the env-var  contains the password of the custom user to create.
    # You can add any env-var in your Kubegres resource config YAML.

    dt=$(date '+%d/%m/%Y %H:%M:%S');
    echo " - Running init script the 1st time Primary PostgreSql container is created...";
    make_db () {
    customDatabaseName="$1"
    customUserName="$2"
    customPassword="$3"
    echo " - Running: psql -v ON_ERROR_STOP=1 --username  --dbname  ...";

    psql -v ON_ERROR_STOP=1 --username "" --dbname "" <<-EOSQL
    CREATE DATABASE ;
    CREATE USER  WITH PASSWORD '';
    GRANT ALL PRIVILEGES ON DATABASE "" to ;
    ALTER DATABASE "" SET bytea_output = 'escape';
    EOSQL
    }

    make_db examplencdb examplenc deadbeef123
   
    echo " - Init script is completed";
apiVersion: kubegres.reactive-tech.io/v1
kind: Kubegres
metadata:
  name: example-postgres
  namespace: example

spec:

   replicas: 1
   image: postgres:17-alpine

   database:
      size: 8000Mi
      storageClassName:  "example-hostpath"

   customConfig: example-postgres-conf

   env:
      - name: POSTGRES_PASSWORD
        valueFrom:
           secretKeyRef:
              name: example-secrets
              key: db-admin-pass
      - name: POSTGRES_REPLICATION_PASSWORD
        valueFrom:
           secretKeyRef:
              name: example-secrets
              key: replicationUserPassword
      - name: POSTGRES_NC_DB_PASSWORD
        valueFrom:
            secretKeyRef:
              name: example-secrets
              key: nc-db-password 
      - name: POSTGRES_OPENPROJECT_DB_PASSWORD
        valueFrom:
            secretKeyRef:
              name: example-secrets
              key: op-db-password 

I have an example repo that can replicate this issue using KinD:
https://github.com/DeployCoop/vigilant-octo-waffle/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions