Py学习  »  Django

Django迁移操作失败,错误为“数据库系统处于恢复模式”

StarLord • 4 年前 • 870 次点击  

我正在迁移一个旧项目,但由于某种原因,在这个文件中迁移总是失败,我试着执行这个迁移文件,仍然有同样的错误。

我已经附加了我使用的命令,命令上来自python终端的日志和下面数据库的日志。

使用的命令

python3 manage.py migrate content 0037_auto_20180618_1711

迁移文件

# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2018-06-18 17:11
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('content', '0036_trivia_original_description'),
    ]

    operations = [
        migrations.AlterField(
            model_name='trivia',
            name='is_approved',
            field=models.CharField(blank=True, max_length=32, null=True),
        ),
    ]

迁移操作的日志

> File
> "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py",
> line 65, in execute
>     return self.cursor.execute(sql, params) psycopg2.OperationalError: server closed the connection unexpectedly
>         This probably means the server terminated abnormally
>         before or while processing the request.
> 
> 
> The above exception was the direct cause of the following exception:
> 
> Traceback (most recent call last):   File
> "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py",
> line 244, in apply_migration
>     state = migration.apply(state, schema_editor)   File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/migration.py",
> line 129, in apply
>     operation.database_forwards(self.app_label, schema_editor, old_state, project_state)   File
> "/usr/local/lib/python3.5/dist-packages/django/db/migrations/operations/fields.py",
> line 215, in database_forwards
>     schema_editor.alter_field(from_model, from_field, to_field)   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py",
> line 514, in alter_field
>     old_db_params, new_db_params, strict)   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/postgresql/schema.py",
> line 112, in _alter_field
>     new_db_params, strict,   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py",
> line 683, in _alter_field
>     params,   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py",
> line 120, in execute
>     cursor.execute(sql, params)   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py",
> line 80, in execute
>     return super(CursorDebugWrapper, self).execute(sql, params)   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py",
> line 65, in execute
>     return self.cursor.execute(sql, params)   File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 94,
> in __exit__
>     six.reraise(dj_exc_type, dj_exc_value, traceback)   File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line
> 685, in reraise
>     raise value.with_traceback(tb)   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py",
> line 65, in execute
>     return self.cursor.execute(sql, params) django.db.utils.OperationalError: server closed the connection
> unexpectedly
>         This probably means the server terminated abnormally
>         before or while processing the request.
> 
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):   File
> "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py",
> line 213, in ensure_connection
>     self.connect()   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py",
> line 189, in connect
>     self.connection = self.get_new_connection(conn_params)   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/postgresql/base.py",
> line 176, in get_new_connection
>     connection = Database.connect(**conn_params)   File "/usr/local/lib/python3.5/dist-packages/psycopg2/__init__.py", line
> 130, in connect
>     conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: FATAL:  the database system is in recovery mode
> 
> 
> The above exception was the direct cause of the following exception:
> 
> Traceback (most recent call last):   File "manage.py", line 12, in
> <module>
>     execute_from_command_line(sys.argv)   File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py",
> line 363, in execute_from_command_line
>     utility.execute()   File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py",
> line 355, in execute
>     self.fetch_command(subcommand).run_from_argv(self.argv)   File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py",
> line 283, in run_from_argv
>     self.execute(*args, **cmd_options)   File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py",
> line 330, in execute
>     output = self.handle(*args, **options)   File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/migrate.py",
> line 204, in handle
>     fake_initial=fake_initial,   File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py",
> line 115, in migrate
>     state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)   File
> "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py",
> line 145, in _migrate_all_forwards
>     state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)   File
> "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py",
> line 244, in apply_migration
>     state = migration.apply(state, schema_editor)   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py",
> line 95, in __exit__
>     self.atomic.__exit__(exc_type, exc_value, traceback)   File "/usr/local/lib/python3.5/dist-packages/django/db/transaction.py",
> line 267, in __exit__
>     connection.set_autocommit(True)   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py",
> line 401, in set_autocommit
>     self.ensure_connection()   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py",
> line 213, in ensure_connection
>     self.connect()   File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 94,
> in __exit__
>     six.reraise(dj_exc_type, dj_exc_value, traceback)   File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line
> 685, in reraise
>     raise value.with_traceback(tb)   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py",
> line 213, in ensure_connection
>     self.connect()   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py",
> line 189, in connect
>     self.connection = self.get_new_connection(conn_params)   File "/usr/local/lib/python3.5/dist-packages/django/db/backends/postgresql/base.py",
> line 176, in get_new_connection
>     connection = Database.connect(**conn_params)   File "/usr/local/lib/python3.5/dist-packages/psycopg2/__init__.py", line
> 130, in connect
>     conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: FATAL:  the database system is in recovery mode

PostgreSQL数据库中的日志如下

postgres_1    | 2019-01-27 15:19:32.361 UTC [1] LOG:  database system is ready to accept connections
postgres_1    | 2019-01-27 16:31:15.062 UTC [1] LOG:  server process (PID 100) was terminated by signal 11: Segmentation fault
postgres_1    | 2019-01-27 16:31:15.062 UTC [1] DETAIL:  Failed process was running: ALTER TABLE "content_trivia" ALTER COLUMN "is_approved" TYPE varchar(32) USING "is_approved"::varchar(32), ALTER COLUMN "is_approved" DROP NOT NULL
postgres_1    | 2019-01-27 16:31:15.062 UTC [1] LOG:  terminating any other active server processes
postgres_1    | 2019-01-27 16:31:15.062 UTC [97] WARNING:  terminating connection because of crash of another server process
postgres_1    | 2019-01-27 16:31:15.062 UTC [97] DETAIL:  The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
postgres_1    | 2019-01-27 16:31:15.062 UTC [97] HINT:  In a moment you should be able to reconnect to the database and repeat your command.
postgres_1    | 2019-01-27 16:31:15.063 UTC [101] FATAL:  the database system is in recovery mode
postgres_1    | 2019-01-27 16:31:15.064 UTC [1] LOG:  all server processes terminated; reinitializing
postgres_1    | 2019-01-27 16:31:15.090 UTC [102] LOG:  database system was interrupted; last known up at 2019-01-27 15:19:32 UTC
postgres_1    | 2019-01-27 16:31:15.175 UTC [102] LOG:  database system was not properly shut down; automatic recovery in progress
postgres_1    | 2019-01-27 16:31:15.184 UTC [102] LOG:  redo starts at 0/1B00C58
postgres_1    | 2019-01-27 16:31:15.184 UTC [102] LOG:  invalid record length at 0/1B00C90: wanted 24, got 0
postgres_1    | 2019-01-27 16:31:15.184 UTC [102] LOG:  redo done at 0/1B00C58
postgres_1    | 2019-01-27 16:31:15.232 UTC [1] LOG:  database system is ready to accept connections

不确定特定sql命令失败的原因。 ALTER TABLE "content_trivia" ALTER COLUMN "is_approved" TYPE varchar(32) USING "is_approved"::varchar(32), ALTER COLUMN "is_approved" DROP NOT NULL

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43848
 
870 次点击  
文章 [ 1 ]  |  最新文章 4 年前
2ps
Reply   •   1 楼
2ps    5 年前

它可能是一个包含大量数据的表,因此更改该表会导致操作使postgres耗尽数据。如果不需要保留表中的数据,请使用 truncate table 语句。如果需要保留数据,请使用 pg_dump ,清除它,运行迁移,然后重新加载数据。