Create or update existing postgres db container through kubernetes job Create or update existing postgres db container through kubernetes job kubernetes kubernetes

Create or update existing postgres db container through kubernetes job


You have to mount the SQL file as a volumen from a configmap and use the psql cli to execute the commands from mounted file.

To execute commands from file you can change the command parameter on the yaml by this:

psql -a -f sqlCommand.sql

The configmap needs to be created using the file you pretend to mount more info here

kubectl create configmap sqlCommands.sql --from-file=sqlCommands.sql

Then you have to add the configmap and the mount statement on your job yaml and modify the command to use the mounted file.

apiVersion: batch/v1kind: Jobmetadata:  name: init-dbspec:  template:    metadata:      name:  init-db      labels:        app: init-postgresdb    spec:      containers:      - image: "docker.io/bitnami/postgresql:11.5.0-debian-9-r60"        name: init-db        command: [ "bin/sh", "-c", "psql -a -f /sqlCommand.sql" ]        volumeMounts:        - name: sqlCommand          mountPath: /sqlCommand.sql        env:          - name: DB_HOST            value: "knotted-iguana-postgresql"          - name: DB_DATABASE            value: "postgres"      volumes:        - name: sqlCommand          configMap:          # Provide the name of the ConfigMap containing the files you want          # to add to the container          name: sqlCommand.sql      restartPolicy: OnFailure


You should make a docker file for the same first, execute it and map the same working docker image to the kubernetes job yaml file.

You can add an entrypoint.sh in docker file, where you can place your scripts to be executed