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

查看:90
本文介绍了为什么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天全站免登陆