🌐
Stack Overflow
stackoverflow.com › questions › 64259293 › change-postgres-config-using-kubernetes-configmap
postgresql - Change Postgres config using Kubernetes configMap - Stack Overflow
October 8, 2020 - apiVersion: v1 kind: ConfigMap metadata: name: pg-hba-configmap data: pg_hba.conf: |+ # TYPE DATABASE USER ADDRESS METHOD # Some personal settings here --- apiVersion: apps/v1 kind: StatefulSet metadata: name: db spec: selector: matchLabels: app: db serviceName: db replicas: 1 template: metadata: labels: app: db spec: nodeSelector: db: 'true' containers: - name: db image: kartoza/postgis:10.0-2.4 volumeMounts: - name: data-volume mountPath: /var/lib/postgresql - name: pg-hba-config-volume mountPath: /var/lib/postgresql/10/main/pg_hba.conf subPath: pg_hba.conf volumes: - name: data-volume hostPath: path: /data/db - name: pg-hba-config-volume configMap: name: pg-hba-configmap
🌐
Bobcares
bobcares.com › blog › postgres-kubernetes-configmap
Postgres Kubernetes ConfigMap | How to Deploy
February 21, 2024 - apiVersion: v1 kind: ConfigMap metadata: name: postgres data: POSTGRES_DB: myapp_production · Kubernetes Secrets can be expressed via a manifest.
🌐
Red Hat
developers.redhat.com › learning › learn:openshift:build-and-populate-database-using-kubernetes-init-containers › resource › resources:create-postgresql-instance-configmap-and-deployment-objects
Create the PostgreSQL instance, ConfigMap, and Deployment objects | Build and populate a database using Kubernetes init containers | Red Hat Developer
February 17, 2026 - Likewise, you can view the Kubernetes documentation related to configuring a pod to use a ConfigMap. When those two pieces of knowledge are combined, you get a deployment that, in our case, includes the following (an explanation follows this section of the YAML file): spec: initContainers: - name: init-createdb image: postgres:14 command: ["psql", "postgresql://postgres:postgres@postgresql", "-f", "/etc/rsalbums/create_database.sql"] volumeMounts: - name: albums-volume mountPath: /etc/rsalbums - name: init-builddb image: postgres:14 command: ["psql", "postgresql://postgres:postgres@postgresql/
🌐
Eksworkshop
developers.eksworkshop.com › introduction to kubernetes › initializing postgresql database with kubernetes configmaps
Initializing PostgreSQL Database with Kubernetes ConfigMaps | EKS Developers Workshop
This lab guided you through the process of creating a Kubernetes ConfigMap that securely initializes your PostgreSQL database within a Minikube environment.
🌐
Kubegres
kubegres.io › doc › override-default-configs.html
Override default configs
For each namespace where a Kubegres resource is created, Kubegres controller creates a base ConfigMap resource with this contents. That base ConfigMap contains the default configs for all Kubegres resource(s) in that namespace. It defines default contents for "postgres.conf", "pg_hba.conf" and a backup logic in "backup_database.sh".
🌐
GitHub
github.com › kubernetes › kubernetes › issues › 70241
config map vs secret to store credentials for Postgres deployment · Issue #70241 · kubernetes/kubernetes
October 25, 2018 - - env: - name: POSTGRES_DB valueFrom: configMapKeyRef: name: postgres-config key: POSTGRES_DB - name: POSTGRES_USER valueFrom: configMapKeyRef: name: postgres-config key: POSTGRES_USER - name: POSTGRES_PASSWORD valueFrom: configMapKeyRef: name: postgres-config key: POSTGRES_PASSWORD
Author   AndriiOmelianenko
🌐
Readthedocs
postgres-operator.readthedocs.io › en › stable › reference › operator_parameters
Configuration parameters - Postgres Operator - Read the Docs
In that case, the operator defines a pod toleration according to the values of those keys. See kubernetes documentation for details on taints and tolerations. The default is empty. pod_environment_configmap namespaced name of the ConfigMap with environment variables to populate on every pod.
🌐
CloudyTuts
cloudytuts.com › guides › kubernetes › how-to-deploy-postgress-kubernetes
How to Deploy Postgres on Kubernetes - CloudyTuts
September 8, 2020 - Data stored in a ConfigMap not encrypted. These Kubernetes resources should be only be used with data that is non-sensitive. apiVersion: v1 kind: ConfigMap metadata: name: postgres data: POSTGRES_DB: myapp_production
🌐
GitHub
github.com › zalando › postgres-operator › blob › master › manifests › configmap.yaml
postgres-operator/manifests/configmap.yaml at master · zalando/postgres-operator
Postgres operator creates and manages PostgreSQL clusters running in Kubernetes - postgres-operator/manifests/configmap.yaml at master · zalando/postgres-operator
Author   zalando
🌐
VMware
techdocs.broadcom.com › us › en › vmware-tanzu › data-solutions › tanzu-for-postgres-on-kubernetes › 3-0 › tnz-postgres-k8s › configure-postgresql-server.html
Customizing the PostgreSQL Server - Broadcom TechDocs
January 23, 2026 - apiVersion: v1 kind: ConfigMap metadata: name: my-postgresql-configmap labels: app: postgres data: log_truncate_on_rotation: "on" log_rotation_age: "1d" log_filename: "postgresql-%A.log" log_rotation_size: "0" logging_collector: "on" Once Kubernetes updates the volume mount, it applies your changes, and the pods will be restarted based on the parameter Restart field value:
Find elsewhere
🌐
Medium
medium.com › @xcoulon › managing-pod-configuration-using-configmaps-and-secrets-in-kubernetes-93a2de9449be
Managing Pod configuration using ConfigMaps and Secrets in Kubernetes | by Xavier Coulon | Medium
November 28, 2017 - $ kubectl exec -it postgres-3585693371-xnzjv bash root@postgres-3585693371-xnzjv:/# psql -U user -d url_shortener_db psql (9.6.5) Type "help" for help.url_shortener_db=# ConfigMaps can also be defined by a manifest with data in a key/value form, but for the sake of example, we’ll load the content from an external file, config.yaml which contains a single entry (cough!) to set the logger level:
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-deploy-postgres-to-kubernetes-cluster
How to Deploy Postgres to Kubernetes Cluster | DigitalOcean
March 25, 2026 - kubectl create secret generic postgres-auth \ --namespace postgres-demo \ --from-literal=POSTGRES_PASSWORD='ReplaceWithStrongPassword' ... apiVersion: v1 kind: ConfigMap metadata: name: postgres-config namespace: postgres-demo data: POSTGRES_DB: appdb POSTGRES_USER: appuser
🌐
Medium
jineshnagori.medium.com › ultimate-guide-to-setting-up-postgresql-in-kubernetes-with-statefulsets-adminer-dashboard-and-6232323cb4dc
Ultimate Guide to Setting Up PostgreSQL in Kubernetes with StatefulSets, Adminer Dashboard, and Ingress. | by Jinesh Nagori | Medium
August 29, 2023 - apiVersion: apps/v1 kind: StatefulSet metadata: name: postgres spec: serviceName: postgres replicas: 3 selector: matchLabels: app: postgres template: metadata: labels: app: postgres spec: containers: - name: postgres image: postgres:10.1 imagePullPolicy: "IfNotPresent" ports: - containerPort: 5432 envFrom: - configMapRef: name: postgres-config env: - name: POSTGRES_PASSWORD valueFrom: secretKeyRef: name: postgres-secret key: POSTGRES_PASSWORD volumeMounts: - mountPath: /var/lib/postgresql/data name: postgredb volumes: - name: postgredb persistentVolumeClaim: claimName: postgres-pv-claim · Services in Kubernetes are the objects that pods use to communicate with each other.
🌐
BMC Software
bmc.com › blogs › kubernetes-postgresql
Deploying PostgreSQL as a StatefulSet in Kubernetes – BMC Software | Blogs
July 2, 2021 - So, let’s create a ConfigMap and modify the StatefulSet YAML as shown below. # PostgreSQL StatefulSet ConfigMap apiVersion: v1 kind: ConfigMap metadata: name: postgres-db-config labels: app: postgresql-db data: POSTGRES_DB: testdb POSTGRES_USER: testdbuser POSTGRES_PASSWORD: testdbuserpassword PGDATA: /data/pgdata
Top answer
1 of 2
6

I realized there were a few mistakes here after posting this question...

  1. I used PostgreSQL 11 for replication prior so I assumed they worked the same way (which of course is wrong, there are some changes). The recovery.conf is omitted from PostgreSQL 12 and it gave this error message FATAL: XX000: using recovery command file "recovery.conf" is not supported when I had it. so I had to remove it from my ConfigMap.

  2. I confused about Docker's Entrypoint & Command to Kubernetes' Command & Args. After being corrected by my senior that Kubernetes Command will override the Docker Entrypoint, I'm going to need and use only Args afterwards.

The following are the changes I made to my ConfigMap and Deployment.

apiVersion: v1
kind: ConfigMap
metadata:
  name: pg-replica
  labels:
    app: postgres
    name: pg-replica
data:
  POSTGRES_DB: postgres
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: mypassword
  pg_hba.conf: |
    # Contents
  postgresql.conf: |
    data_directory = '/var/lib/postgresql/data'
    # the contents from recovery.conf are integrated into postgresql.conf
    primary_conninfo = # host address and authentication credentials
    promote_trigger_file = # trigger file path
  extra.sh: |
    #!/bin/sh
    postgres -D /var/lib/postgresql
---
apiVersion: v1
kind: Service
metadata:
  name: pg-replica
  labels:
    app: postgres
    name: pg-replica
spec:
  type: NodePort
  ports:
  - nodePort: 31000
    port: 5432
  selector:
    app: postgres
    name: pg-replica
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pg-replica
spec:
  selector:
    matchLabels:
      app: postgres
      name: pg-replica
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
        name: pg-replica
    spec:
      containers:
        - name: pg-replica
          image: postgres:latest
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: pg-replica
          volumeMounts:
            - name: pg-replica
              mountPath: /var/lib/postgresql/data
            - name: replica-config
              mountPath: /var/lib/postgresql/postgresql.conf
              subPath: postgresql.conf
            - name: replica-config
              mountPath: /var/lib/postgresql/pg_hba.conf
              subPath: pg_hba.conf
            - name: replica-config
              mountPath: /docker-entrypoint-initdb.d/extra.sh
              subPath: extra.sh
          args:
            - "-c"
            - "config_file=/var/lib/postgresql/postgresql.conf"
            - "-c"
            - "hba_file=/var/lib/postgresql/pg_hba.conf"
      volumes:
        - name: pg-replica
          persistentVolumeClaim:
            claimName: pv-replica-claim
        - name: replica-config
          configMap:
            name: pg-replica

The arguments in Args will set the location of the .conf files to where I specified.

For further steps in Replication:

  1. After the pod is up, I manually ran the pod's shell with kubectl exec.

  2. I removed all the files from the data-directory for step 3 (to copy files from master pod).

rm -rf /var/lib/postgresql/data/*
  1. Use pg_basebackup to backup data from master node.
pg_basebackup -h <host IP> --port=<port number used> -D  /var/lib/postgresql/data -P -U replica -R -X stream

And that's it. Now I managed to have my pg-replica pod replicating my master pod.

2 of 2
1

As mentioned in comments, I really encorage you to use the Postgres Helm chart to setup yout environment.

The way you solved the issue could work, but if the pod died for some reason, all work you have done will be lost and you'll need to reconfigure everything again.

Here you can found all information about how to create a postgres deployment with high availability and replication.

To install HELM you can follow this guide.

🌐
GitHub
github.com › multigres › multigres-operator
GitHub - multigres/multigres-operator: Kubernetes operator for Multigres — deploys, scales, and manages horizontally scalable PostgreSQL clusters with automated topology orchestration, drain-safe rolling updates, and admission webhooks
3 weeks ago - PostgreSQL Configuration: Reference a user-created ConfigMap with postgresql.conf overrides via postgresConfigRef on shard templates.
Starred by 235 users
Forked by 23 users
Languages   Go 98.2% | Makefile 1.0% | JavaScript 0.4% | Shell 0.2% | Python 0.1% | Dockerfile 0.1%
🌐
Adam the Automator
adamtheautomator.com › postgres-to-kubernetes
How to Deploy Postgres to Kubernetes
April 27, 2022 - You’ll create a Kubernetes ConfigMap to store database details such as user, database name, and password for the PostgreSQL deployment.
🌐
Refine
refine.dev › home › blog › engineering › how to deploy postgres on kubernetes
How to deploy Postgres on Kubernetes | Refine
January 22, 2024 - This mounts the postgresql.conf from the ConfigMap into the PostgreSQL container at the specified mountPath. B. Securing PostgreSQL Using Kubernetes Secrets
🌐
GitHub
gist.github.com › nathanborror › 36ebcb42472775b1aa4a8edc135ee615
Example Kubernetes setup with Postgres and two Services for serving an API and a static site using Ingress. Also have a CronJob example for kicks. · GitHub
April 12, 2022 - Example Kubernetes setup with Postgres and two Services for serving an API and a static site using Ingress. Also have a CronJob example for kicks. - instructions.txt