2

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?!

6
  • 1
    Please show the full traceback. Commented Feb 21, 2018 at 13:44
  • @Alasdair I think migrations files is incorrect. Commented Feb 21, 2018 at 13:54
  • @Alasdair I remove "delta_device" migrations files and then do ./manage.py migrate, and well done. Commented Feb 21, 2018 at 13:56
  • Hi Benyamin, I have added the complete answer. I would be thankful if you could accept my answer and give upvote to it. Commented Jan 28, 2020 at 13:28
  • 1
    @MostafaGhadimi Hello, Ok I would. However, the main problem was about a migrations file that its solution mentioned in the above comment by Alasdair. Also, this question was asked about 2 years ago! Commented Jan 28, 2020 at 16:56

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

Comments

1

I removed "delta_device" migrations files, and then:

python manage.py migrate

and done well.

Thanks to @Alasdair

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.