Notes on my journey learning to code with Python & Django
AMartin1987 | Published on: May 30, 2023, 4:47 p.m.
Finally, I converted my database (intially in SQLite3, as its Django's default db format) to PostgreSQL! So I guess it's ready for the next step in my learning project, which is hosting this very blog at Vercel 😊
So I used the migrate.py commands dumpdata
and loaddata
. These were the steps I followed:
1) I got sure my site was running ok at localhost with settings.py configured for a sqlite3 database.
2) I stopped running my project and did python manage.py dumpdata <name_of_my_app> > data.json --indent 2
This dumped all tables and data from my previous sqlite3 db into a new fixture (a collection of data that Django knows how to import into a database) named "data", which is in json format with an indentation of 2 for better reading. I previously had tried to do the same in a sql format but got some errors, then I found someone in stackoverflow who said it was not possible to dump data in that format.
3) As referenced in the article Dump and Load Data in Django, i did python manage.py flush
to reset my current database data (but not the table structure).
4) I installed PostgreSQL, and created a new db. I got the settings for that database (the ones that postgreSQL assigns by default), that is, the name of the db, the user, password, host and port. I used those data to configure settings.py for a PostgreSQL db:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '<name_of_your_blog>', //as installed in 'INSTALLED_APPS'
'USER': '<your_user_in_postgres>', //by default it's 'postgres'
'PASSWORD': '<your_postgres_db_password>',
'HOST': 'localhost', //default value
'PORT': 5432, // default value
}
}
5) It's not secure to display these data in here, so I had previously installed a Python package to include .env files in my app (django-environ) to store environmental variables with the secret values that we don't want other people to read, for instance, when you upload the project to your GitHub repository. Then i created an standard .ignore file which indicates that this .env file shouldn't be included when you push your commits to your repository.
The settings.py configuration for my new postgreSQL db reads now like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': env('PGDATABASE'),
'USER': env('PGUSER'),
'PASSWORD': env('PGPASSWORD'),
'HOST': env('PGHOST'),
'PORT': env('PGPORT'),
}
}
6) I did python manage.py loaddata data.json.
Here I got an error because it seems by default dumpdata created an utf-16 json file, but can only load an utf-8 file. I just saved the file as utf-8 json with Notepad
😆. Then it loaded fine.
7) That's it! Now this blog is running locally on a postgreSQL db. As I said, next step is getting it to load on the internet. I've been familiarizing with PostgreSQL documentation as well, which is pretty clearly written.
Ale 💜🌿
PS: I'm finding ChatGPT and Google's Bard to be incredible teachers (in combination with the classic method of searching for answers in stackoverflow :P )
My name is Alejandra and I'm learning Python & Django to become a backend developer. I also love videogames, anime, plants and decorating my home.
You can contact me at alejandramartin@outlook.com
You can also visit my portfolio.