Why doesn't postgres create the database?

3/6/2021

I have problems in deploying a simple postgres container on minikube. It doesn't create "postgres" database when a persistent volume is mounted:

kubectl exec -it POSTGRES_POD -- sh

/ $ su postgres
/ $ psql
psql: FATAL:  database "postgres" does not exist

Here is my postgres deployment:

resource "kubernetes_deployment" "backend_postgres" {
  metadata {
    ...
  }

  spec {
    replicas = 1

    ...

    template {
      metadata {
        ...
      }

      spec {
        volume {
          name = "data"

          persistent_volume_claim {
            claim_name = kubernetes_persistent_volume_claim.data.metadata[0].name
          }
        }

        container {
          name  = "postgres"
          image = "postgres:10-alpine"

          port {
            container_port = 5432
          }

          env {
            name = "POSTGRES_PASSWORD"

            value_from {
              secret_key_ref {
                name = kubernetes_secret.postgres_password.metadata[0].name
                key  = "password"
              }
            }
          }
	
          volume_mount {
            name       = "data"
            mount_path = "/var/lib/postgresql/data"
            sub_path   = "postgres"
          }

        }
      }
    }
  }
}

The persistent volume successfully gets mounted and postgres successfully writes to /var/lib/postgresql/data but as I mentioned earlier, it doesn't create "postgres" database.

But when I don't mount a persistent volume, everything works fine.

Here is my deployment without mounting any persistent volume:

resource "kubernetes_deployment" "backend_postgres" {
  metadata {
    ...
  }

  spec {
    replicas = 1

    ...

    template {
      metadata {
        ...
      }

      spec {

        container {
          name  = "postgres"
          image = "postgres:10-alpine"

          port {
            container_port = 5432
          }

          env {
            name = "POSTGRES_PASSWORD"

            value_from {
              secret_key_ref {
                name = kubernetes_secret.postgres_password.metadata[0].name
                key  = "password"
              }
            }
          }
	
        }
      }
    }
  }
}

I'm working on this strange scenario for days.

I'm runing the kubernetes cluster on minikube. Any kind of help or suggestion would be really appreciated.

-- HsnVahedi
docker
kubernetes
minikube
postgresql
terraform

1 Answer

3/6/2021

Since it is using a persistent volume, it presumably won't be recreating the database instance each time, but rather re-using the one that already exists. And somehow, the one that already exists isn't configured the way you want it to be.

After making sure that the database is not running anywhere, you could move it all someplace else (because you may discover you actually need it for something) leaving the persistent directory empty. Then try again and see if it creates a suitable database instance complete with the expected "postgres" database.

-- jjanes
Source: StackOverflow