[英]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:考虑到我们有两个集群
production
和staging
,我的方法如下:
db_test
in the staging Postgres serverdb_test
db_test
will be periodically synced with db_prod
. db_test
将定期与db_prod
同步。 To do so: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 连接的)。 Desired behavior:期望的行为:
pg_dump
and pg_restore
are successful and the new database is cleared of customer information pg_dump
和pg_restore
成功,新数据库清除客户信息Actual behaviour:实际行为:
pg_dump
and pg_restore
are successful. pg_dump
和pg_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
clean_db
without the migrate
beforemigrate
的clean_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"```
migrate
commandmigrate
命令的错误日志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.