Hosting PostgreSQL database on Supabase for free
Hello, welcome to devmaesters website. In this post I am going to be guiding you through the process of hosting your postgreSQL database on supabase for free using supabase free tier plan.
Introduction
Due to Heroku's decision to stop providing free dynaos for thier free tier plan alot of Django web developers have been searching for other free alternatives to host their websites. One of the awesome platforms I found from my research is vercel hosting platform which allows you to host your django website for free(checkout this post to learn how its done). One of the major drawback of this platform is that unlike heroku they does not provide a database attachement when you host on their platform so to solve that issue I made more research and discovered supabase .This platform is a firebase alternative that offers many features which I recommend you to checkout on their website but for the purpose of this post we are going to concentrate on the fact that their free plan gives us access to hosting 2 postgreSQL database for free with a 500mb data limit on each. Checkout below to see all of superbase plans;
As you can see supabase offers a free plan that provides 500mb data storage using postgreSQL and 1gb file storage per project. One drawback from this platform is that free projects gets paused after a week of inactivity. But that should'nt be an issue as the free plan is to be used mostly for testing out our projects in a production enviroment and it takes less than 5 mins to unpaused the projects.
Ps if your postgreSQL database gets accessed atleaset once a week your project will remain active and unpaused
Creating an account on supabase
Go to @supabase and signup for free tier plan account (I recommend signing up with github).
After signing up, login to the account and you would be shown a dashboard similar to the one below;
Create an organisation
The way supabase works you have to create an organisation and then within that organisation you can create different projects. So follow the instructions and create an organisation on superbase
Create a project
- Click on new project and select the organisation you created in the previous step
- Add your project name
- Type in a strong password or use the generate password feauture to auto generate one for you
- There are several regions available select the one closest to you
- In the pricing field leave it at free-$0/month
- Finally click on create project and then wait a couple of minutes for your project to be created for you.
If the project was created successfully you should see a page similar to the one below;
Like I said earlier supabase has many awesome features which include
- Auth: for managing users for your project
- Storage: for storing large files (the free tier plan comes with free 1gb storage)
For this post I am going to focus on using the database feature of this project.
Database configurations
What we need to be able to use this progreSQL database for our website are listed below
- Password => password for your database
- User => username for your database
- Port => port for your database(default used by poasgres is 5432)
- Name => name associated with your database
- Host => the host credential for your database
- Engine => connection engine for your database(default using psycopg2 is django.db.backends.postgresql_psycopg2)
From the image in the create a project step above you can see a left hand side navigation bar and towards the bottom there is a settings icon click on it to be able to access the setting for your project.
Next click on Database from the menu that appears to acces the database configurations. You will see a page similar to the one below
Copy your Database host, Database name, Database port, Database user, and Database password(i.e its the password you used to create the project).
Go to your projects settings.py file and paste in the values you copied above in their respective postions in your database configuration
DATABASES = {
'default': {
'ENGINE': "Database engine",
'HOST': "Database host",
'NAME': "Database name",
'USER': "Database user ",
'PASSWORD': "Database password",
'PORT': "Database port",
}
}
Press Ctr + s to save your changes
Migrate your model structure to the databse
The next step is migrate all your models and settings to the database hosted on superbase. Please before starting this step ensure that you are connected to the internet and stop your development server if you have one running.
Execute the commands below in your terminal
python manage.py makemigrations
and
python manage.py migrate
Give it a minute to migrate all your models to the database. After its done go back to your supabase project dashboard.
On the left hand navigation bar below the home icon there is a an icon for table editor. Click on it to get access to the table editor page. Your page should contain something similar to the one below;
Within this editor you can see all the tables in your database and make changes to them if you wish.
Finally redeploy your project to vercel and it will now work using the database you just created on superbase.
Please note that in this post I hardcoded the database configurations in settings.py. This is not good coding practice, I advice that you should store sensitive values like database configurations in enviroment variables.
If you already have a postgreSQL database hosted somewhere else and you will like to migrate all your data to supabase then check out this post Migrating your PostgreSQL database from one hosting platform to another (e.g Heroku to Supabase) to learn how its done.
Conclusion
We have been able to successfully host our postgreSQL database for a django project for free using supabase. I would to love to hear your thoughts and ideas on the post so feel free to leave a comment down below. As always thanks for reading and please do subscribe to my youTube channel to get access to video tutorials on various programming topics.