Benyamin Jafari
Benyamin Jafari

Reputation: 34006

migration from sqlite to postgresql in 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': '',
}

Or:

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

But when do python manage.py migrate I encountered by this error:

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

full traceback:

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

What's wrong?!

Upvotes: 2

Views: 2568

Answers (2)

Mostafa Ghadimi
Mostafa Ghadimi

Reputation: 6736

I have decided to explain the instructions from scratch:

  1. Install Postgres on your computer.

    • 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.
  2. Access to Postgres using sudo su - postgres command.

  3. Create a new database. createdb <dbname>

  4. Create a database user (with password). createuser -P <username>

  5. Access the shell using psql command.

  6. Grant this new user access to your new database with GRANT ALL PRIVILEGES ON DATABASE <dbname> TO <username>; command.

  7. Dump existing data. python3 manage.py dumpdata > datadump.json

  8. Install Postgres package. pip install psycopg2

  9. Change settings.py configuration to the following:

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

  1. Make sure you can connect to Postgres DB. python3 manage.py migrate --run-syncdb

  2. 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()
  1. Finally, load your data. python3 manage.py loaddata datadump.json

Upvotes: 6

Benyamin Jafari
Benyamin Jafari

Reputation: 34006

I removed "delta_device" migrations files, and then:

python manage.py migrate

and done well.

Thanks to @Alasdair

Upvotes: 1

Related Questions