<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aiden Ellis</title>
    <description>The latest articles on DEV Community by Aiden Ellis (@aidenellis).</description>
    <link>https://dev.to/aidenellis</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F629879%2F056e6836-a343-4917-8e15-2bcac8553884.jpg</url>
      <title>DEV Community: Aiden Ellis</title>
      <link>https://dev.to/aidenellis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aidenellis"/>
    <language>en</language>
    <item>
      <title>Upload Existing Django Sqlite database to heroku.</title>
      <dc:creator>Aiden Ellis</dc:creator>
      <pubDate>Wed, 12 May 2021 08:46:46 +0000</pubDate>
      <link>https://dev.to/aidenellis/upload-existing-django-sqlite-database-to-heroku-1e4e</link>
      <guid>https://dev.to/aidenellis/upload-existing-django-sqlite-database-to-heroku-1e4e</guid>
      <description>&lt;p&gt;So, for a few days i've been struggling to upload my old database which was sqlite3 to heroku's Postgres database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Now here comes the challanges:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Convert old sqlite3 database to postgresql with all of &lt;br&gt;
 the existing data into it&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Upload that postgresql database to heroku&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This might look easy but it really takes too much time and effort to find the way to do it.And in this post i'm gonna help you out here, to the mooooon! ;)&lt;/p&gt;



&lt;h3&gt;
  
  
  1.Converting database
&lt;/h3&gt;

&lt;p&gt;To convert the database (sqlite3) to postgresql, first we &lt;br&gt;
need &lt;a href="https://www.postgresql.org/download/" rel="noopener noreferrer"&gt;Postgresql&lt;/a&gt; and &lt;a href="https://www.pgadmin.org/download/" rel="noopener noreferrer"&gt;PG Admin&lt;/a&gt; installed in our machine.Go and download and install these 2. &lt;br&gt;
&lt;code&gt;NOTE: While installing PG Admin it will ask you for a passwords, make sure to remember that password.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we are going to create an empty Postgres Database.&lt;br&gt;
Head into PG Admin, Enter the password that when it ask's for.And follow these steps to create an empty database.&lt;/p&gt;
&lt;h4&gt;
  
  
  1.1) Click on &lt;code&gt;Objects&lt;/code&gt; in the top.
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_1.jpg" title="step 1" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  1.2) Then click on &lt;code&gt;create&lt;/code&gt;.
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_2.jpg" title="step 2" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  1.3) Now we gotta create a group name.In this case i named it Tutorial
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_3.jpg" title="step 3" alt="alt text"&gt;&lt;/a&gt;&lt;br&gt;
click on &lt;code&gt;save&lt;/code&gt;.Now in the left you can see out new group 'Tutorial'.&lt;/p&gt;
&lt;h4&gt;
  
  
  1.4) Now let's create the server.Right click on that 'Tutorail' Group.Then Create &amp;gt; Server
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_4.jpg" title="step 4" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  1.5) Fill up the Name, I named it 'CONVERT_DB'
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_5.jpg" title="step 5" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  1.6) Now click on Connection and fill up 'hostname', 'password'
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupstorage.pythonanywhere.com%2Fapi%2Fstorage%2Ffile%2Fits_sakib%2FPublic%2Fdev.to%2Fposts%2Fpost_1%2Fstep_6.jpg" title="step 6" alt="alt text"&gt;&lt;/a&gt;&lt;br&gt;
Here type &lt;code&gt;localhost&lt;/code&gt; in host name/address and in the password input type the password that you gave PG Admin while installing it.And Click SAVE.&lt;/p&gt;

&lt;p&gt;There You go we just created an empty server.&lt;/p&gt;



&lt;br&gt;
Now to transfer all of the sqlite3 data to our empty postgresql database.We first have to create a dumpfile from sqlite3
&lt;h4&gt;
  
  
  1.7) Make sure that you have set the sqlite3 database in your settings.py
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;default&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ENGINE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.db.backends.sqlite3&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;NAME&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BASE_DIR&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;old_database_name.sqlite3&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  1.8) Create datadump.json from manage.py
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 manage.py dumpdata &amp;gt; datadump.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will create a datadump.json file.&lt;br&gt;
&lt;code&gt;Note: Sometime, this generates broken json file so you have to format it yourself if that json file shows any error.Even if you have print('') in settings.py it will add it to the json file.This is a bug so you have to deal with it.&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  1.9) Now we are going to change the DATABASE to the postgresql server.so head on to settings.py
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;default&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ENGINE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.db.backends.postgresql&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;NAME&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CONVERT_DB&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# The Server name from 1.5
&lt;/span&gt;        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;USER&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;postgres&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# The username from 1.6
&lt;/span&gt;        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;PASSWORD&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;secretpassword&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;# The password from installation
&lt;/span&gt;        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;HOST&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;localhost&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;# Host name/address from 1.6,
&lt;/span&gt;        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;PORT&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;5432&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;# Port from 1.6
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Set the information the the server information. from (1.6)&lt;/p&gt;
&lt;h4&gt;
  
  
  1.9.1) Now migrate the empty database with --run-syncdb.
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 manage.py migrate --run-syncdb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  1.9.2) Go to shell using manage.py
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python3&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;shell&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  1.9.3) Now as you are in shell type these commands.
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib.contenttypes.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ContentType&lt;/span&gt;
&lt;span class="n"&gt;ContentType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;quit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  1.9.4) Finally! It's time to load our old data to this new database
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python3&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;loaddata&lt;/span&gt; &lt;span class="n"&gt;datadump&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will load the data from our old database to the new database using the datadump.json file that we created from sqlite3.&lt;/p&gt;

&lt;p&gt;Auhhhh! What a relief.We converted the data but, we still got 1 more challange left which is Uploading our new database (with old data) in to heroku.Lets goooo (My cat is on my keyboard xD)&lt;/p&gt;



&lt;h3&gt;
  
  
  2.Uploading the postgres database in heroku
&lt;/h3&gt;

&lt;p&gt;Now as you know that you can't upload file to Heroku through their website, You can upload with github but thats a worst way you can do it with risking your data.&lt;/p&gt;

&lt;p&gt;So we are going to upload the database through heroku CLI.Open Your command prompt and type (Make sure your PG Admin in running.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pg_dump -Fc --no-acl --no-owner -h localhost -U USERNAME DATABASE_NAME &amp;gt; mydb.dump
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the 'USERNAME' is the name from the database from (1.6).Defaultly it was set to postgres and i didn't chage it in my case, DATABASE_NAME is the database 'Server name' from (1.5) where i set it to 'CONVERT_DB'.&lt;br&gt;
So in my case the command will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pg_dump -Fc --no-acl --no-owner -h localhost -U postgres CONVERT_DB &amp;gt; mydb.dump
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;This will ask for a passwords: which is the password from installation&lt;/code&gt;&lt;br&gt;
After you enter the password This will create a mydb.dump file.So if you've got through this far then congrats we are really close.And if you got any error just give a comment below.&lt;/p&gt;

&lt;p&gt;now that we have our mydb.dump file.We can upload it to heroku but to upload it we have to do it via some online file storage service.You have to store the 'mydb.dump' file to the cloud and make it public to it's accessible from anywhere.&lt;/p&gt;

&lt;p&gt;You can upload it to aws, or any other file server.But the link should be the direct download link, like if you paste it to the browser it will automatically start downloading. Now it could be overwelming and it's a bit hard since there's no easy way for free.If you got stuck here then just comment below i will try to help you out with the cloud storing easily. :)&lt;/p&gt;

&lt;p&gt;And now we have the dump_file_link we can upload it to heroku&lt;br&gt;
our imaginary dump_file_link is '&lt;a href="https://some_cloud_server/path/to/file.dump" rel="noopener noreferrer"&gt;https://some_cloud_server/path/to/file.dump&lt;/a&gt;'.&lt;/p&gt;

&lt;p&gt;Before we upload make sure you have heroku cli installed and are logged in.Make sure to delete the old database from heroku and create a new fresh database there.&lt;/p&gt;

&lt;h4&gt;
  
  
  2.0) Open command prompt and type
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku pg:backups:restore &amp;lt;'dump_file_link'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will ask for you conformation and you have to enter your app name to confirm and KABOOM!!! You'r database is now updated.&lt;/p&gt;

&lt;p&gt;PS: I just started using heroku a few days ago and im still figuring out stuffs.If i weren't able to help then im sorry for that but i actually tried my best to solve it in a post an d you know its hard to solve overwelming problems in just a post.Anyway if you made it so far Congratulations!!!!!! Now i'll go and take a nap with my cat. see ya ;) Meow uwu&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
