Skip to content

Instantly share code, notes, and snippets.

@paulpwo
Last active June 13, 2021 20:14
Show Gist options
  • Save paulpwo/e2f839a8736965d67af0bf8ca75511da to your computer and use it in GitHub Desktop.
Save paulpwo/e2f839a8736965d67af0bf8ca75511da to your computer and use it in GitHub Desktop.
Django | Migrar SQLite y postgres

**Step 1:

Make a backup of your project (It does not hurt to prevent any eventuality)

**Step 2:

Run a data export. Directly Django provides the mechanism. The method that did not give me problems was the following:

python manage.py dumpdata --natural-foreign --natural-primary --format = xml --indent = 2> fixture.xml

The use of the XML file and the --natural-foreign --natural-primary flags was the difference between the failure and the success of the operation. If you omit these flags you could get a lot of errors in the console.

**Step 3:

Remember to install the Postgress dependency so that Django can work with this database engine:

pip install psycopg2

**Step 4:

Change your database connection from SQLite to postgress in your settings.py file. It could be something like the following:

default ': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'dbname',
        'USER': 'dbuser',
        'PASSWORD': 'dbpassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }

** Step 5:

Delete all migration files. That is, everything that is inside the my_module / migrations folder. It is important not to delete the init.py file

This must be repeated in each module of your app that contains potential migrations.

** Step 6:

Run the build of migrations and then migrate to configure your Postgress database. In this way it will be a valid template to contain the data that you will import later:

python manage.py makemigrations
python manage.py migrate

** Step 7:

Load the data into your new postgress database with the following command:

 python manage.py loaddata fixture.xml

You can now test your app with this database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment