简体   繁体   English

Kubernetes cronjob 为 Django 应用程序转储和恢复 postgres 数据库时出错

[英]Error in Kubernetes cronjob to dump and restore postgres database for Django application

I need to create a testing environment for a specific team to use our Django application for testing against the database.我需要为特定团队创建一个测试环境,以使用我们的 Django 应用程序对数据库进行测试。 This database must be somewhat in sync with out production database.该数据库必须与生产数据库有点同步。 Considering we have two clusters production and staging , my approach is the following:考虑到我们有两个集群productionstaging ,我的方法如下:

  • Create a db_test in the staging Postgres server在登台 Postgres 服务器中创建一个db_test
  • db_test will be periodically synced with db_prod . db_test将定期与db_prod同步。 To do so:这样做:
  • Create a cronjob in the staging cluster that connects to the production database, do a pg_dump and then do a pg_restore to the db_test (using localhost because it's connected through a pgbouncer).在连接到生产数据库的staging集群中创建一个 cronjob,执行pg_dump ,然后对db_test执行pg_restore (使用localhost因为它是通过 pgbouncer 连接的)。
  • I run the migrations to make sure the tables are up to date我运行迁移以确保表是最新的
  • Next: I need to run a management command that will erase some customer data from this copy (to be GDPR compliant).下一步:我需要运行一个管理命令,该命令将从该副本中删除一些客户数据(以符合 GDPR)。

Desired behavior:期望的行为:

  • pg_dump and pg_restore are successful and the new database is cleared of customer information pg_dumppg_restore成功,新数据库清除客户信息

Actual behaviour:实际行为:

  • pg_dump and pg_restore are successful. pg_dumppg_restore成功。 I can psql into the newly created database and everything looks alright.我可以psql进入新创建的数据库,一切看起来都很好。
  • migrate command fails with the traceback below migrate命令失败,回溯如下
  • clean_db fails because it can't find some tables (which they exist as I inspected with psql . clean_db失败,因为它找不到一些表(它们在我用psql检查时存在。

This is the simple shell script I run in the cronjob:这是我在 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
  • This is the error of the clean_db without the migrate before这是之前没有migrateclean_db的错误
relation "uploader_somemodel" does not exist
2
LINE 1: SELECT COUNT(*) AS "__count" FROM "uploader_somemodel...

But this is the log of the table being created但这是正在创建的表的日志

pg_restore: processing data for table "public.uploader_somemodel"```
  • And this is the error log of the migrate command这是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...
                     ^

Has anyone ever encountered a similar error and know what this is about?有没有人遇到过类似的错误并知道这是怎么回事? Or even better, do you have a suggestion of how I could perform this diferently?或者更好的是,您对我如何以不同的方式执行此操作有什么建议吗?

Did you change the NAME of the db in your env variable ?您是否更改了 env 变量中数据库的名称? To get this result in your django settings.py:要在您的 django settings.py 中获得此结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django + Postgres:正在尝试转储和还原数据库,但是看到错误:并非所有序列表都存在“ * _id_seq”关系 - Django + Postgres: Trying dump and restore database, but are seeing ERROR: relation “*_id_seq” does not exist for all sequence tables 如何在 django 中转储 postgres 数据库? - how to dump postgres database in django? django postgres重新加载转储错误 - django postgres reload dump error Postgres/python django 导入 json 转储数据库 - Postgres/python django import json dump database 无法在 heroku 上恢复 postgres 转储 - Cannot restore postgres dump on heroku 由于数据库所有权差异,无法还原Django应用程序的Postgres数据库 - Unable to restore postgres database of Django app because of database ownership discrepancies 通过Django从托管在其他服务器中的数据库中获取Postgres转储 - Get Postgres dump from a database hosted in a different server through Django Dotcloud,Postgres Django上的数据库错误 - Database Error on Dotcloud, Postgres Django 如何仅使用 heroku 上的 postgres 转储来恢复我的数据库数据 - How can I restore my database data with only postgres dump on heroku 从Mysql切换到Django应用程序的Postgres数据库时,Apache mod_wsgi错误 - Apache mod_wsgi error when switching from Mysql to Postgres database for Django application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM