为什么 postgres 不创建数据库? [英] Why doesn't postgres create the database?

查看:44
本文介绍了为什么 postgres 不创建数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 minikube 上部署一个简单的 postgres 容器时遇到问题.它不会创建postgres"挂载持久卷时的数据库:

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

这是我的 postgres 部署:

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"
          }

        }
      }
    }
  }
}

持久卷成功挂载并且 postgres 成功写入 /var/lib/postgresql/data 但正如我之前提到的,它不会创建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.

我在 minikube 上运行 kubernetes 集群.任何形式的帮助或建议将不胜感激.

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

推荐答案

由于它使用的是持久卷,因此大概不会每次都重新创建数据库实例,而是重新使用已经存在的实例.不知何故,已经存在的那个并没有按照您希望的方式进行配置.

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.

在确保数据库没有在任何地方运行后,您可以将它全部移到其他地方(因为您可能会发现您实际上需要它来做某事),将持久目录留空.然后再试一次,看看它是否创建了一个合适的数据库实例,并带有预期的postgres".数据库.

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.

这篇关于为什么 postgres 不创建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆