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:
- pg_dump: https://docs.postgresql.fr/13/app-pgdump.html
- pg_restore/psql: https://docs.postgresql.fr/13/app-pgrestore.html
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
.
Dump data in the
data.json
file.python manage.py dumpdata > data.json
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'
}
}
python manage.py migrate --run-syncdb
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')