Allen Kim
Allen Kim

Reputation: 59

Postgres db name and username are not applied on kubernetes

I'm using Kubernetes and trying to connect server container to postgres container.

Putting "postgres" for POSTGRES_USER and POSTGRES_DB is working. However, when values other than "postgres" are inserted as environment values for postgres username and database name, it seems that somehow those values are overwritten as "postgres" or those values might have not been applied at all.

Is there a way to fix this problem?

postgres-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres
    spec:
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim
      containers:
        - name: postgres
          image: postgres
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
              subPath: postgres
          env:
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: dbpassword
                  key: DB_PASSWORD

server-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: server
  template:
    metadata:
      labels:
        component: server
    spec:
      containers:
        - name: server
          image: ellybundle_deployment_api
          imagePullPolicy: Never
          ports:
            - containerPort: 4000
          env:
            - name: DB_HOST
              value: postgres-cluster-ip-service
            - name: DB_USERNAME
              value: postgres
            - name: DB_PORT
              value: "5432"
            - name: DB_NAME
              value: postgres
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: dbpassword
                  key: DB_PASSWORD

What is not working - changed the dbname and db-user

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres
    spec:
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim
      containers:
        - name: postgres
          image: postgres
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
              subPath: postgres
          env:
            - name: POSTGRES_USER
              value: test-user
            - name: POSTGRES_DB
              value: test-db-name
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: dbpassword
                  key: DB_PASSWORD
apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: server
  template:
    metadata:
      labels:
        component: server
    spec:
      containers:
        - name: server
          image: ellybundle_deployment_api
          imagePullPolicy: Never
          ports:
            - containerPort: 4000
          env:
            - name: DB_HOST
              value: postgres-cluster-ip-service
            - name: DB_USERNAME
              value: test-user
            - name: DB_PORT
              value: "5432"
            - name: DB_NAME
              value: test-db-name
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: dbpassword
                  key: DB_PASSWORD

Upvotes: 0

Views: 1277

Answers (3)

Philipp
Philipp

Reputation: 1

I had the same problem and deleting the pvc was not enough.

I used the Kubernetes Cluster on Docker Desktop for local development and changing Postgres credentials did not apply on my Postgres Node. I had to reset Docker to factory defaults. This worked for me.

Docker Desktop Factory Reset

Upvotes: 0

Nepomucen
Nepomucen

Reputation: 6667

Looks like your PostgreSQL setup is already initialized, and values of ENV VARIABLES are taken from postgresql.conf file. Check for its existence and content under /var/lib/postgresql/data path.

Also worth to know, if your PostgreSQL Docker container image is anyhow basing on Bitnami's one:

Note! When POSTGRESQL_USERNAME is specified, the postgres user is not assigned a password and as a result you cannot login remotely to the PostgreSQL server as the postgres user. If you still want to have access with the user postgres, please set the POSTGRESQL_POSTGRES_PASSWORD environment variable (or the content of the file specified in POSTGRESQL_POSTGRES_PASSWORD_FILE).

Link to the resource above.

Upvotes: 0

vishnu narayanan
vishnu narayanan

Reputation: 3953

POSTGRES_PASSWORD environment variable sets the superuser password for PostgreSQL.

I see that you are using persistent volume claims in your postgres-deployment.yaml. Are you deleting your volumes before recreating your deployment with a new password?

Check existing persistent volume claims(PVC)

$ kubectl get pvc

Delete PVC

$ kubectl delete pvc <postgres-pvc-name>

Now recreate your deployment after setting a new password via POSTGRES_PASSWORD.

Note: you will lose all data in Postgres once you delete the volume claim.

Upvotes: 0

Related Questions