Exporting and importing data in Django using pg_dump in postgresql, mysqldump in mysql, dumpdata and loaddata commands in Django

(Comments)

In this tutorial, I will explain how to export and import data in a Django project.

Data export/import of a Django project simply means the export/import of the data present in the database that the Django project uses.

Lets talk about the export/import of two popular databases and then directly from Django:

1. PostgresSql
2. MySql

Exporting and importing data from a PostgresSql database:

Exporting:

Syntax:

pg_dump -U <db_username> <db_name> -h <host> -t <table_name> > exported_data.sql

Example for exporting with database user 'myuser', database name 'db_name', host 'localhost':

pg_dump -U myuser db_name -h localhost > exported_data.sql

If the host is a remote ip (1.2.3.4 for example), then

pg_dump -U myuser db_name -h 1.2.3.4 > exported_data.sql

Exporting a single table can be done using "-t table_name" as follows:

pg_dump -U myuser db_name -h 1.2.3.4 -t table_name > exported_data.sql

Importing:

Syntax:

psql -U <db_username> <db_name> -h <host> < exported_data.sql

Example for importing with database user 'myuser', database name 'db_name', host 'localhost':

pg_dump -U myuser db_name -h localhost < exported_data.sql

If the host is a remote ip (1.2.3.4 for example), then

pg_dump -U myuser db_name -h 1.2.3.4 < exported_data.sql

Exporting/importing data from a MySql database:

Exporting:

Syntax:

mysqldump -u <db_username> <db_name> <table_name> --password > exported_mysql_data.sql

Example for exporting with database user 'myuser', database name 'db_name', host 'localhost' or 127.0.0.1 or any remote host:

mysqldump -u myuser db_name  -h localhost --password > exported_mysql_data.sql

If the host is a remote ip (1.2.3.4 for example), then

mysqldump -u myuser db_name -h 1.2.3.4 --password > exported_mysql_data.sql

Exporting a single table can be done using as follows:

mysqldump -u myuser db_name table_name -h 1.2.3.4  > exported_mysql_data.sql

Importing:

Syntax:

mysql -u <db_username> <db_name> -h <host> --password < exported_mysql_data.sql

Example for importing with database user 'myuser', database name 'db_name', host 'localhost':

mysql -u myuser db_name -h localhost --password < exported_mysql_data.sql

If the host is a remote ip (1.2.3.4 for example), then

mysql -u myuser db_name -h 1.2.3.4 --password < exported_mysql_data.sql

Exporting and Importing data from a Django project

Django provides two management commands for this purpose

1. dumpdata
2. loaddata

Management command: Dumpdata

The management command 'dumpdata' dumps the data in the JSON format. This JSON data can be exported to any Django project irrespective of the database behind it (in most cases).

python manage.py dumpdata > dumped_data.json

If you want the exported file to have a readable JSON, then you can use "--indent". It will export data in JSON format with the specified indentation.

python manage.py dumpdata --indent=4 > dumped_data.json

Management command: Loaddata

The management command 'loaddata' loads the data that we just exported. Note that the data that we export from postgresql, mysql databases using pg_dump, mysqldump cant be loaded here and vice versa.

Here is the syntax:

python manage.py loaddata < dumped_data.json

Comments

Recent Posts

Archive

2022
2021
2020
2019
2018
2017
2016
2015
2014

Tags

Authors

Feeds

RSS / Atom