(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
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
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
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
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
Django provides two management commands for this purpose
1. dumpdata 2. loaddata
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
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
We develop web applications to our customers using python/django/angular.
Contact us at hello@cowhite.com
Comments