【问题标题】:Error in Kubernetes cronjob to dump and restore postgres database for Django applicationKubernetes cronjob 为 Django 应用程序转储和恢复 postgres 数据库时出错
【发布时间】:2021-12-23 02:47:54
【问题描述】:

我需要为特定团队创建一个测试环境,以使用我们的 Django 应用程序对数据库进行测试。该数据库必须在某种程度上与生产数据库同步。考虑到我们有两个集群productionstaging,我的方法如下:

  • 在暂存 Postgres 服务器中创建 db_test
  • db_test 将定期与 db_prod 同步。为此:
  • 在连接到生产数据库的staging 集群中创建一个cronjob,执行pg_dump,然后对db_test 执行pg_restore(使用localhost,因为它是通过pgbouncer 连接的)。
  • 我运行迁移以确保表是最新的
  • 下一步:我需要运行一个管理命令,从该副本中删除一些客户数据(以符合 GDPR)。

期望的行为:

  • pg_dumppg_restore 成功并清除新数据库中的客户信息

实际行为:

  • pg_dumppg_restore 成功。我可以psql 进入新创建的数据库,一切正常。
  • migrate 命令失败,回溯如下
  • clean_db 失败,因为它找不到一些表(在我使用 psql 检查时它们存在。

这是我在 cronjob 中运行的简单 shell 脚本:

#!/bin/bash
# Dump the database locally
pg_dump --host=mydb.postgres.database.azure.com \
        --username=myuser@production \
        --no-owner \
        --verbose \
        -Ft db_prod > $HOME/db_prod-$(date +%F).tar &&

sleep 30 &&

# Restore the database
pg_restore --no-owner \
    --no-acl \
    --host='localhost' \
    --user=myuser@staging \
    --port=5432 \
    --clean \
    --dbname=db_test \
    $HOME/db_prod-$(date +%F).tar \
    --verbose &&

python manage.py migrate &&

python manage.py clean_db
  • 这是clean_db之前没有migrate的错误
relation "uploader_somemodel" does not exist
2
LINE 1: SELECT COUNT(*) AS "__count" FROM "uploader_somemodel...

但这是正在创建的表的日志

pg_restore: processing data for table "public.uploader_somemodel"```
  • 这是migrate 命令的错误日志
Running all migrations...
Operations to perform:
  Apply all migrations: admin, auth, axes, contenttypes, django_mfa, myapp, reversion, sessions, uploader, userapi
Running migrations:
Traceback (most recent call last):
  File "/opt/venv/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 67, in ensure_schema
    editor.create_model(self.Migration)
  File "/opt/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 307, in create_model
    self.execute(sql, params or None)
  File "/opt/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 137, in execute
    cursor.execute(sql, params)
  File "/opt/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/opt/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/opt/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/opt/venv/lib/python3.8/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/opt/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: no schema has been selected to create in
LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA...
                     ^

有没有人遇到过类似的错误并且知道这是怎么回事?或者更好的是,您对我如何以不同的方式执行此操作有什么建议吗?

【问题讨论】:

    标签: django postgresql django-models


    【解决方案1】:

    您是否更改了环境变量中数据库的名称? 在你的 django settings.py 中得到这个结果:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': db_test,
            'USER': POSTGRES_USER,
            'PASSWORD': POSTGRES_PASSWORD,
            'HOST': POSTGRES_HOST,
            'PORT': 5432,
        }
    }
    

    【讨论】:

    • 是的。环境变量在 cronjob 中设置。例如,我可以进入容器并执行 Django shell,但其他命令惨遭失败
    猜你喜欢
    • 2020-06-17
    • 2011-04-28
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    相关资源
    最近更新 更多