Home > front end >  Django Migratons: What is the best default way to dump and load data in DB apart from using third pa
Django Migratons: What is the best default way to dump and load data in DB apart from using third pa

Time:09-02

I'm new to Django and interested in knowing in-depth about migrations so I don't want to use the third-party apps(ex. flyway..)

I want to know how to dump hundreds of data in my Postgres DB without using 3rd party applications.

CodePudding user response:

You can directly use postgresql tools:

CodePudding user response:

there is a feature called fixtures which you can use: this is an example of dumping data

django-admin dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]]

CodePudding user response:

This is what I have tried to migrate my database from db.sqlite3 to PostgreSQL.

  1. Dump data in the data.json file.

    python manage.py dumpdata > data.json

  2. Edit the settings.py file of your project and change the database settings. (In my case I have used Heroku's PostgreSQL. You can get all the necessary details from the respective database providers (AWS, Heroku, etc).

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': ' ', 
'USER':' ', 
'PASSWORD':' ’, 
'HOST':' ',
'PORT':'5432'
}
}
  1. python manage.py migrate --run-syncdb

  2. python manage.py loaddata data.json

If errors are encountered:

JSON parsing error:

Make sure the data.json file is saved with utf-16 LE encoding (guessed by VS Code).

Go to:

C:\Users\UserName\AppData\Local\Programs\Python\Python39\Lib\sitepackages\django\core
serializers\json.py

Replace:

 stream_or_string = stream_or_string.decode()

With:

 stream_or_string = stream_or_string.decode('UTF-16')
  • Related