How to migrate data from SQLite to PostgreSQL in Django
Tags: python, django, tutorial, beginners
Published: 2019-10-11 20:59:26

This post cross-published with [OnePublish](https://www.onepubli.sh)

<p>Hi DEVs! In this post I want to talk about differences between SQLite and PostgreSQL. In addition, I will show you how to migrate your data from SQLite to PostgreSQL.</p>

<p>Well, SQLite and PostgreSQL are the most widely used relational database management systems. They are both open-source and free. However, they have some major differences that should be considered when choosing a database to use for your applications.</p>

<p>There is a lot articles on internet which covers this topic, so I will not focus on comparison of these databases.</p>
<a href="https://tableplus.com/blog/2018/08/sqlite-vs-postgresql-which-database-to-use-and-why.html">SQLite vs PostgreSQL - Which database to use and why?</a>

<a href="https://www.digitalocean.com/community/tutorials/sqlite-vs-mysql-vs-postgresql-a-comparison-of-relational-database-management-systems">SQLite vs MySQL vs PostgreSQL: A Comparison Of Relational Database Management Systems</a>

<p>SQLite is too "light" for real world applications, so majority of developers prefer to use MySQL or PostgreSQL for web applications. Generally, SQLite is highly useful for:</p>

<ul>
<li>Standalone apps</li>
<li>Small apps that don’t require expansion</li>
<li>Apps need to read or write files to disk directly</li>
<li>The internet of things devices</li>
<li>Developing and even testing</li>
</ul>

<p>and PostgreSQL is recommended when:</p>
<ul>
<li>Data integrity and reliability is highly concerned.</li>
<li>Custom Procedures which is extensible to run the complex task.</li>
<li>Complexity with ease. PostgreSQL gives you the functionality to maintain such a complex database smoothly without limitations.</li>
</ul>

<h3>Migrate data from SQLite to PostgreSQL</h3>
<p>Dump existing data:</p>
```
python3 manage.py dumpdata > datadump.json
```
<p>Change settings.py to Postgres backend. Check this awesome tutorial by Digital Ocean:</p>
<a href="https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04">How To Use PostgreSQL with your Django Application on Ubuntu</a>

<p>Make sure you can connect on PostgreSQL. Then:</p>
```
python3 manage.py migrate --run-syncdb
```
<p>Run this on Django shell to exclude contentype data</p>
```
python3 manage.py shell
```
```
>>> from django.contrib.contenttypes.models import ContentType
>>> ContentType.objects.all().delete()
>>> quit()
```
<p>Finally, run following command to load the json data:</p>

```
python3 manage.py loaddata datadump.json
```

<p>Great! Now, your all data migrated from SQLite to PostgreSQL.</p>

<p>There is also interesting question on StackOverflow about MySQL vs PostgreSQL.</p>
{% stackoverflow 585549 %}

<p>That's it! Make sure you are following me on social media and if you find it useful please share with your friends. See you in next post DEVs!</p>

<a href="https://reversepython.net/">Reverse Python</a>
<a href="https://www.instagram.com/coderasha/?hl=en">Instagram</a>
<a href="https://twitter.com/coderasha?lang=en">Twitter</a>