从sqlite迁移到Django中的postgresql [英] migration from sqlite to postgresql in django
问题描述
我想从 sqlite 迁移到 postgresql 数据库:
我已经安装了postgresql并在其shell上创建了db,然后将django设置配置为以下:
I want to migrate from sqlite to postgresql db:
I was installed postgresql and create db on its shell, then configure my django setting as bellow:
'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 migration
遇到此错误:
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
怎么了?!
推荐答案
我决定从头开始解释说明:
I have decided to explain the instructions from scratch:
-
在计算机上安装Postgres.
Install Postgres on your computer.
- 首先安装
sudo apt-get install libpq-dev python-dev
,它们是Postgres依赖项,可以完美地与Django配合使用. - 然后,输入
sudo apt-get install postgresql postgresql-contrib
命令以安装Postgres.
- First install
sudo apt-get install libpq-dev python-dev
which are Postgres dependencies to work with Django perfectly. - Then, enter
sudo apt-get install postgresql postgresql-contrib
command to install Postgres.
使用 sudo su-postgres
命令访问Postgres.
Access to Postgres using sudo su - postgres
command.
创建一个新的数据库. createdb< dbname>
Create a new database. createdb <dbname>
创建一个数据库用户(使用密码). createuser -P< username>
Create a database user (with password). createuser -P <username>
使用 psql
命令访问外壳程序.
Access the shell using psql
command.
使用 GRANT ALL PRIVILEGES ON DATABASE< dbname>授予该新用户对您的新数据库的访问权限.TO< username> ;;
命令.
转储现有数据. python3 manage.py dumpdata>datadump.json
安装Postgres软件包. pip install psycopg2
Install Postgres package. pip install psycopg2
将settings.py配置更改为以下内容:
Change settings.py configuration to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '<dbname>',
'USER': '<username>',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '',
}
}
-
确保可以连接到Postgres DB.
python3 manage.py migration --run-syncdb
在Django shell上运行此命令以排除内容类型数据.
Run this on Django shell to exclude contentype data.
python3 manage.py shell
>>> from django.contrib.contenttypes.models import ContentType
>>> ContentType.objects.all().delete()
>>> quit()
- 最后,加载您的数据.
python3 manage.py loaddata datadump.json
这篇关于从sqlite迁移到Django中的postgresql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!