gpt4 book ai didi

python - Django 中从 sqlite 迁移到 postgresql

转载 作者:行者123 更新时间:2023-12-01 02:07:46 25 4
gpt4 key购买 nike

我想从 sqlite 迁移到 postgresql 数据库:
我安装了 postgresql 并在其 shell 上创建数据库,然后配置我的 django 设置如下:

'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'dbpass',
'HOST': 'localhost',
'PORT': '',
}

或者:

'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'dbpass',
'HOST': 'localhost',
'PORT': '',
}

但是什么时候python manage.py迁移我遇到了这个错误:

ValueError: invalid literal for int() with base 10: '0x000

完整回溯:

Operations to perform:
Apply all migrations: delta_device, admin, menu, sessions, datapipeline, datacollector, siemens_s7, contenttypes, auth, settings
Running migrations:
Rendering model states... DONE
Applying delta_device.0002_auto_20171210_1631...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 482, in alter_field
old_db_params, new_db_params, strict)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 565, in _alter_field
new_default = self.effective_default(new_field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 210, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 720, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1853, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: '0x000

怎么了?!

最佳答案

我决定从头开始解释这些说明:

  1. 在您的计算机上安装 Postgres。

    • 首次安装 sudo apt-get install libpq-dev python-dev这是与 Django 完美配合的 Postgres 依赖项。
    • 然后,输入sudo apt-get install postgresql postgresql-contrib安装 Postgres 的命令。
  2. 使用 sudo su - postgres 访问 Postgres命令。

  3. 创建一个新数据库。 createdb <dbname>

  4. 创建数据库用户(带密码)。 createuser -P <username>

  5. 使用 psql 访问 shell命令。

  6. 使用 GRANT ALL PRIVILEGES ON DATABASE <dbname> TO <username>; 授予此新用户对新数据库的访问权限命令。

  7. 转储现有数据。 python3 manage.py dumpdata > datadump.json

  8. 安装 Postgres 软件包。 pip install psycopg2

  9. 将 settings.py 配置更改为以下内容:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '<dbname>',
'USER': '<username>',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '',
}
}

  • 确保您可以连接到 Postgres 数据库。 python3 manage.py migrate --run-syncdb

  • 在 Django shell 上运行此命令以排除内容类型数据。

  • python3 manage.py shell

    >>> from django.contrib.contenttypes.models import ContentType
    >>> ContentType.objects.all().delete()
    >>> quit()
  • 最后,加载您的数据。 python3 manage.py loaddata datadump.json
  • 关于python - Django 中从 sqlite 迁移到 postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48907467/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com