How to Deploy a Django App and Postgres Database to Render
Summary
TLDRThis video provides a step-by-step guide on how to deploy a Django app along with a PostgreSQL database to the Render platform. The presenter explains how to set up an account, connect GitHub, and create a PostgreSQL database on Render. Key steps include configuring environment variables, adjusting settings in Django, handling deployment errors, and connecting the Django app to the database. The video also touches on debugging, deploying with custom domains, and troubleshooting common deployment issues. It's designed to demonstrate an easy and practical approach to deploying a Django project.
Takeaways
- 🚀 Render is an easy-to-use service for deploying web applications and databases.
- 🔗 The first step in deploying on Render is connecting your GitHub account to easily deploy code from a repository.
- 🐍 In the example project, a simple Django app is deployed, demonstrating how to add 'Members' to a PostgreSQL database.
- 💾 Instead of using SQLite, the video shows how to switch to PostgreSQL by creating a database on Render and connecting it to the Django app.
- 🔧 The database connection settings are managed using the 'dj-database-url' package in Django to parse the database URL.
- 💻 The migration process requires installing PostgreSQL's 'psycopg2' package to create tables in the new database.
- 📊 The video demonstrates how to view and manage the database using a tool called DBeaver.
- 🔑 Several settings, including 'debug', 'allowed hosts', and 'secret key', are moved to environment variables for better security and flexibility.
- 🌐 To deploy the Django app on Render, the video explains how to add a start command using 'Gunicorn' to run the app’s WSGI file.
- 🐛 The video highlights common deployment issues, such as mismatched Django versions and host configuration problems, and how to troubleshoot them.
Q & A
What is Render, and how is it used in the video?
-Render is a cloud platform used to deploy various services such as web apps, static sites, databases, and background workers. In the video, Render is used to deploy a Django web app and a PostgreSQL database.
How does the presenter connect the Django app to a PostgreSQL database?
-The presenter creates a PostgreSQL database on Render, copies the external connection URL, and uses the 'dj-database-url' package to configure the Django app to connect to this database. The connection details are then added to the Django app's settings.
What are the main steps to deploy a Django app on Render?
-The main steps include creating a Render account, connecting a GitHub repository with the Django app, configuring environment variables like debug, allowed hosts, and secret key, setting up a PostgreSQL database, and then deploying the app through Render's dashboard.
How does the presenter handle the database migration to the new PostgreSQL database?
-The presenter first updates the database settings to connect to the PostgreSQL database, then runs `python manage.py migrate` to apply the migrations, creating the necessary tables in the new database.
Why does the presenter use environment variables for configuration in the Django settings?
-The presenter uses environment variables to keep sensitive information, like the secret key and database URL, out of the codebase and the repository. This is a common practice for security and flexibility, allowing different configurations for different environments.
What issues does the presenter encounter during the deployment, and how are they resolved?
-The presenter faces issues like incorrect Django version compatibility and an invalid allowed hosts setting. These are resolved by adjusting the Django version in the requirements file and correcting the allowed hosts environment variable.
Why does the presenter switch from using SQLite to PostgreSQL?
-The presenter switches from SQLite to PostgreSQL because PostgreSQL is a more robust and scalable database suitable for production, whereas SQLite is generally used for development purposes.
How is the Django app's secret key managed in the deployment process?
-The Django app's secret key is managed using an environment variable in Render. The key is generated and stored as an environment variable, ensuring it is not hardcoded in the source code.
What command is used to install the required Python packages during deployment?
-The command `pip install -r requirements.txt` is used to install the required Python packages during deployment. This ensures that all the necessary dependencies, including Django and PostgreSQL drivers, are installed.
How does the presenter debug issues with the app after deployment?
-The presenter temporarily enables debug mode by setting the debug environment variable to 'true'. This provides more detailed error messages, allowing them to identify and fix the issues, such as incorrect allowed hosts.
Outlines
🚀 Deploying a Django App on Render with PostgreSQL Integration
This paragraph introduces the video, where the creator demonstrates deploying a Django app on Render, along with setting up a PostgreSQL database. The host explains how Render's dashboard allows easy deployment of web services, databases, and more. After connecting their GitHub account to Render, they showcase a simple Django app that manages 'Members' in a database. The app currently uses an SQLite database, but the goal is to switch to PostgreSQL, showing the steps required to connect the new database in Render.
🔗 Setting Up and Connecting PostgreSQL Database
The focus here is on setting up the PostgreSQL database on Render. The user creates the database through Render's dashboard, specifies the region and version, and uses the free plan. They explain the difference between internal and external database connection URLs. The external connection URL is copied to the code, and the host demonstrates how to replace SQLite settings with PostgreSQL by using the 'dj-database-url' package for easier configuration. They run migrations and successfully connect the app to the PostgreSQL database, verifying this through a tool called DBeaver.
⚙️ Working with Database and Running the Django App Locally
The host uses DBeaver to view the tables generated by the Django app in the PostgreSQL database and verifies that no data exists yet. They then start the app locally, adding members ('Anthony,' 'Herbert,' and 'Pretty Printed') to the database. Afterward, they refresh DBeaver to confirm that the data has been successfully added. The section highlights that the app is connected to Render’s database. The creator also mentions future plans to use the internal database URL for deployment on Render.
💻 Preparing for Deployment and Environment Variable Setup
In this section, the creator begins setting up the Django app for deployment by initializing a Git repository and discussing the importance of environment variables. They explain how to set debug mode, allowed hosts, and secret keys as environment variables in the `settings.py` file. They also cover configuring the database connection to read from environment variables. The host saves all changes to GitHub, demonstrating the need for a `requirements.txt` file to specify Python dependencies for Render to install during deployment.
📂 Finalizing GitHub Setup and Deploying the App on Render
Here, the host finalizes the GitHub setup, pushing the code and dependencies (in `requirements.txt`) to a new repository. They explain the steps to link the GitHub repo to Render, specifying the branch, runtime (Python 3), and build commands. The host installs `Gunicorn` to serve the Django app and configures the deployment settings in Render’s dashboard. They also discuss choosing the free plan for hosting and anticipating possible deployment issues, emphasizing that deployment is often iterative with adjustments required.
🐞 Debugging Common Deployment Issues
The deployment faces version compatibility issues with Django. The creator explains how to resolve these by downgrading Django to version 3.2.19. They highlight the importance of debugging deployment problems and adjusting configurations (like Django version) as needed. The step-by-step approach to resolving issues helps viewers understand the challenges and solutions during the deployment process.
🔧 Environment Variables and Fixing Errors
The deployment progresses, but the app runs into errors related to missing environment variables (secret key, allowed hosts, and debug mode). The host demonstrates adding these environment variables via Render’s dashboard and provides a detailed explanation of each setting. After adding the internal database URL, they show how to trigger a redeployment. They also demonstrate enabling Django’s debug mode temporarily to identify specific errors, such as an incorrect allowed hosts configuration, and explain how to fix these issues.
🌐 Final Testing and Custom Domain Setup
The app is finally live after troubleshooting the deployment issues. The host verifies the app by adding new members and checking that the data is saved correctly. They also explain how to disable debug mode once everything is working. The paragraph ends with a brief introduction to setting up a custom domain for the app on Render, though this step isn’t demonstrated fully in the video.
👍 Conclusion and Final Remarks
The host wraps up the video by thanking viewers and encouraging them to leave comments if they have questions. They highlight that the video focused on deploying a Django app to Render and resolving common deployment issues, including working with PostgreSQL. They also encourage viewers to subscribe for more content.
Mindmap
Keywords
💡Django
💡Render
💡PostgreSQL
💡Environment Variables
💡GitHub
💡Database Migration
💡WSGI
💡Requirements.txt
💡Deployment
💡Secret Key
💡Debug
Highlights
Introduction to deploying a Django app to Render and connecting it with a PostgreSQL database.
Render is an easy-to-use service for deploying web services, static sites, cron jobs, PostgreSQL, and more.
Connecting GitHub to Render allows for seamless deployment of Django apps.
Demonstrating how to switch the database from SQLite to PostgreSQL by setting up a PostgreSQL database on Render.
Using the Django database URL library for easy configuration of database settings in settings.py.
Installing necessary PostgreSQL drivers (psycopg2) to connect Django to the PostgreSQL database.
Verifying the PostgreSQL connection using DBeaver, a tool to interact with databases.
Successfully connecting Django app to PostgreSQL by adding data through the app and seeing the updates in the PostgreSQL database.
Steps to deploy a Django app on Render by connecting the GitHub repository and setting up environment variables for secret key, debug, and allowed hosts.
Using environment variables to secure sensitive data like the secret key and configuring allowed hosts in Django.
Handling common deployment issues, such as incorrect Django versions, by troubleshooting and downgrading to a compatible version.
Setting up the Gunicorn server to run Django's WSGI application on Render for production deployment.
Debugging errors related to allowed hosts by updating environment variables during the deployment process.
Adding new members through the deployed app and verifying that the data is successfully saved in the connected PostgreSQL database.
Instructions for setting a custom domain for the deployed Django app on Render, improving its URL accessibility.
Transcripts
hey everyone in today's video I'm going
to show you how to deploy a Django app
to a service called render and along
with a Django app I will deploy a
database as well so you can see how to
connect this postgresql database that I
create to the Django app and then the
Django app will be running on render so
render is a service that is very easy to
use it's very easy to deploy the things
that they have here so I'm on their
dashboard and you see you can deploy
static sites web services so Django
would be a web service background
workers cron jobs postgresql and other
things so the main thing you need to do
after creating an account is to connect
your GitHub account so here my GitHub
login has been connected pretty printed
and that will allow me to deploy the
Django app easily so the app I'm going
to deploy is this which is very simple
this is just for demonstration purposes
so this is a jingle app and it just
allows me to add something I call
Members so I'll just put my last name
here Herbert and we see I've added
Anthony pretty printed in Herbert to the
database and the app looks like this so
I have this render deploy app I have
this example app within the Django
project and in the views.pi I simply a
query for all the members and then I add
a member here by creating it and then
just redirecting to the index so the app
itself is very simple the reason why I
made it this simple is so I can
demonstrate the deployment process not
so much building an app so if I go over
here to settings.pi I can go down to the
database part and this is the thing that
I want to do first so right now I'm
using this sqli database and it's
generating the file here what I want to
do is I want to use a postgresql
database instead so I'll start on render
by creating a postgresql database and
connecting to it here on my local
machine so let me go over to the render
dashboard and I'll click new postgresql
and then I'll just give it a name so
uh let's call this pretty printed Django
render
and then I'll leave the database name to
be randomly generated along with the
user because I'm not too concerned about
those I'll leave the region as origin
the version is 15 that's the latest I'm
not using datadog and I'm using the free
plan here so I'll just click create
database
and in a moment it will create it so
I'll wait for this to be done so now we
see the status is changed to available
the storage status is still pending but
while we wait for that I can go to the
connect information here and explain it
so here I have two tabs one fit internal
connection and one for external
connection so if I'm connecting to the
database from outside of render I want
to use this external connection URL but
if my app is hosted on render as a web
service then I want to use the internal
database URL so I'm going to copy the
external database URL just copy that and
we see the storage is ready now so I
have one gigabyte of storage now go over
to my code and inside of the code I'll
just paste this as a comment so there
are a couple ways you can do this you
can set up everything here so you can
change the engine to be postgresql you
can change the name to be the database
name and then you can put the username
and password but I like to use a Django
database URL so I will install that so
pip install DJ J database URL and then
I'll just go ahead and import that at
the top so import DJ database URL and
then I'll just use it down here so what
I'm going to do is I'm going to
overwrite the existing database settings
output default here
and I'll use that Django database
url.parse and then I can pass in my
URL here
so I'll just cut and paste that into
that okay so I'll remove this comment
and I'll save this and now that I have a
separate database what I'll do is I'll
do python
manage.pi migrate because I need to
create the
tables in the new database and we see I
have an error here and this is simply
because I don't have the postgres driver
installed so I'll install that on my
system it's cycle pg2 binary but on
yours you might be able to get away with
um cycle pg2
without the binary so I'll go ahead and
install that and now I'll run migrate
again
and this time is running and it's not
giving us any errors so while that runs
I'm going to connect to my database
directly with a tool called D Beaver so
let me open that up and then what I can
do is I can connect to it so this is
just a tool to work with databases so
I'll use postgresql as my database and
then I can copy the URL in here and just
take out the things that go into this
certain field so that was the database
name so I'll put it here pretty printed
Django render the host is going to be
everything after the AD Sign
the username is going to be this user
here so let me go ahead and take out the
user
so
I can just grab after the r
and then paste that into username
and then after the colon but up into the
at sign that's going to be the password
and then that should be it so everything
after the ads is the host I can test
connection it looks like it's working so
finish it appears here on the left so
now I can open it and when I do open it
I should be able to see the tables that
were generated from my app so I can open
up databases here schema public tables
and then these are the tables and I have
the example member table here so let's
look at that
because right now
there should be no data in it so there's
no data but now if I go to the running
app so let me go ahead and start the app
first so
python managed up high run server
and then I go over here when I reload
the page we see there are no members so
I'll go ahead and add Anthony as a
member
I'll add Herbert as a member
and I'll add pretty
printed as a member
okay great so I added those three I can
look at the database again and I'll just
refresh this here and we see it has
three entries Anthony Herbert and pretty
print it in there so we know we are now
connected to the render database and
everything is working properly so as you
can see it was very easy to set up the
database and connect to it
so later I'm going to come back and use
this internal connection so I can
connect to the database using this
internal URL and the process is going to
be pretty much the same I'm just
switching out that URL so now let's
think about deploying the Django app so
I'll go back to the dashboard
and I'll click on new here and I want to
create a new web service
so what it wants me to do here is to
connect a repo that I have but I haven't
created a repo for my project yet so
let's go ahead and do that first so I
can go here and I can do get a knit
and I've turned this into a repo and
what I want to do here is I'll skip the
ignoring files but what I want to do in
the settings.pi is I want to set certain
things up to come from environment
variables because I don't want some
information to go in the repo so let's
go through it I'm going to import OS so
I can grab environment variables so one
thing that needs to be an environment
variable is debug so right now it's just
true but I want this to be the value
from the environment variable so what I
can do is I can say something like OS
dot Environ
baguettes and I'm going to get a key
called debug so I'm going to name the
environment variable debug when I go to
create it
and it's going to return false by
default
and I want to check to see if this is
equal to true so debug takes in a
Boolean so I'm going to see if the debug
value itself is a string that's true so
if it's true it's going to equal true
and actually to make this even better I
can lower this and then
but it's a lowercase shoe so if I put
upper cases or all lowercase in the
environment variable this will still
work
so as long as the string in the
environment variable debug is true this
will equal true if it's anything else
then it's going to be false allowed
hosts I can also create an environment
variable for I'll do OS Environ
dot gets and I want to get
allowed posts so I'll just call
everything the same as the setting in
Django and what I'm going to do is I'm
going to split it
by spaces so this should be an S you'll
see me use this when I go to the render
dashboard and the idea here is I can
have multiple hosts separated by spaces
and then it's going to split them and
return a list as you saw before it was a
list and then after it will be a list
and then finally the secret key I can do
something similar
this one is just straightforward I can
do OS and viron git
Secret
okay
so those are the three things that I
wanted to
make environment variables up here and
then the last thing will be the actual
database so
here I can say like database URL equals
OS Environ gets database URL
and then I will
remove the string and replace it with
the variable
so this is a really straightforward way
of setting up the settings.pi there are
many ways of setting this up so don't
think that this is the best way this is
just the way I wanted to do it for this
video's purpose but there are different
approaches to having settings up high
you can have multiple settings.pi you
can use like the Django Environ Library
you can do multiple things but for the
purposes of this video I just wanted to
keep it like this so those are the
things that I need to make environment
variables so now I'll just go ahead and
save everything to the repo so get at
all git commit
first commits and I probably should have
added a git ignore because I have my
virtual environment here so what I can
do is I can actually create the git
ignore
and then I can do git remove
hashed
and then the EnV directory
Cash Dash R the EnV directory
and then in the git ignore I can just do
slash gits
and then I can do git add all again get
commits I removed EnV okay
so now the EnV directory is no longer my
git repos so it shouldn't take so long
to commit next time there are other
things that I could ignore like uh Pi
cash and uh the sqlite file but I'm just
going to leave that like this and the 10
000 here is no longer correct because
I've already committed the repo okay so
now I want to go to GitHub and I want to
create a repository so I'll call this
render
Django example
and then I'll set this to private and
I'll create the repo
and it's going to give me the get remote
ad origin link down here so I'll just
copy that and I'll paste it in here so
git remote ad origin and then I can do
git push origin Master that's going to
push all my code over to GitHub
so that's done I can refresh now we see
this here the EnV directory is gone
because I removed it so now let's go and
do one more thing because this is a
python app I need a requirements.txt so
I have the virtual environment working
on my local machine but when render goes
to deploy this it needs to install all
the same libraries so what I can do
as I can do uh pip freeze and then put
this in requirements.txt
and then I can just add that to my
commits
edit requirements and then just do get
push origin Master again
and now we see when I refresh
the requirements.txt is here so anytime
you update the requirements.txt you'll
need to push it to your repo and then
redeploy the service to render because
it needs to install the new libraries so
that's it for the GitHub site now let me
just refresh this page so my new repo
loads and when it does I can just find
it and hit connect so there it is I'll
hit connect and then I'll give it a name
so Django render example app
uh the region will be Oregon West's
again the branch will be master so if
you had a different branch name you can
use that but in my case it's just Master
the root directory is not relevant for
this uh the runtime will be python3 so
this is a python project and then the
build command is going to be pip install
requirements.txt so that's why I just
generated the requirements.txt and now
we see the Star Command which is going
to actually run the app so I should
install G unicorn
and then I can do pip freeze
requirements.txt again
and then I can add this to the repo
and just push it
and the idea here is I can run this wsgi
so what I can do is I can say G unicorn
and then the name of my app so the name
of my app is render deploy
render deploy Dot wsgi and if there are
any problems with this we'll see it when
it goes to deploy
so the instance type is free once again
but of course you're going to have
another one if you decide to pay
and then I'll create the web service so
that's all I need for right now
and then it's going to try to deploy it
so there are going to be problems with
the deploy and I'll fix them as they
come up to kind of show you how other
things work here okay so the first error
we get is a can't find Django 4.2.2 so
at the time of recording this video
that's the version of Django that I'm
using because we see here in the
requirements.txt it displays as 4.2.2 so
the way to fix this is I can just
downgrade so let me downgrade to four
0.0 and once again I can just
commit everything so because I modify
requirements.txt directly I can just
commit without having to do Pit freeze
so I get commit and then change Django
version
so git push origin master so this is
something you probably won't have to do
but in my case the versions are just out
of date for from wherever it's pulling
the versions so I'll go back here
and now I can do uh manual deploy and
then deploy latest commit so I can start
the process again and once again there
are going to be problems but like I said
we can deal with them as they come up
and it looks like it doesn't even take
4.0 so the latest version here is 3.2.19
so I'll just change it again
so the reason why I show these in the
video these um
failures is because oftentimes when
people go to deploy apps it doesn't work
exactly perfectly the first time around
so I just want to go through the process
of debugging everything so you can see
what's happening so I updated Django
version again so there are very little
differences between Django 3 and Django
4 that's why I can get away with doing
something like this but but normally you
want to keep the version as close to the
latest as possible
so I'll go back here and I'll once again
uh do the manual deploy and let's see
what happens this time because there are
going to be more issues okay so we see
that the build was successful and now
it's deploying so I don't expect the
deploy itself to work but we'll see what
happens when it finishes and just keep
in mind that when you're using the free
plan this is going to be a bit slow
because if you want the fastest you need
to pay for it okay so here I see a loud
host
um is missing and the reason is is
because I haven't specified that in my
environment so what I can do is I can go
over to the environment here
and I want to specify all the
environment variables that I have so I
can add them so one will be secret key
and I can just generate a secret key
here
right so it doesn't really matter what
the secret key is it just needs to be
secret
I'll add another one this will be
allowed hosts
and the value for this one
should be a string
with spaces between the hosts so I'll
just do that in localhost really you
don't need localhost but just to
demonstrate that this is going to be a
string with two things I'll do it like
that
so I can add another one and I need the
debug
so I want the debug here to be false and
I'll add one more for this one I want
the database URL and now we can go back
to our database so I'll just open a new
tab here and I'll go over to the
database that I have so uh the Django
render database and I want to get the
internal connection URL this time copy
that and just paste that into the
environment variable value so now I'll
save this and when you save environment
variables it will start a deploy
automatically for you so let's just wait
and see what happens here okay so it
looks like everything is live so let's
click on the link here and let's see if
we can use the application and we see
here it's now bad requests
and because we see bad requests here we
know something isn't working properly
but we don't know exactly what the issue
is so what we can do is we can go to the
environment variable here we can edit
the debug and we can put that to true so
this will just be a momentary change so
we can debug this and figure out what's
going on so the app is going to deploy
itself again okay so now that this is
running we see a more detailed error
message so we see Django render example
app on render.com is an invalid host
and the reason is is because we put too
much in the environment variable so if I
go to the environment variable here I
have the HTTP s colon slash slash so let
me remove that save it and then let the
app deploy again and hopefully this is
the last issue we have with this deploy
process okay so it's deployed again now
I can refresh here and now we see the
name so everything is working properly
with the app because it's using the same
database as before we already have data
so I can add a new member here and it
will get saved and new member appears
there so everything is working
what I can do is I can change debug back
to false and I just wanted to show you
one more thing I can go down to settings
and they have the option of having a
custom domain I don't have any domains
available but using a custom domain is
pretty simple so you can just follow the
instructions here and you'll have your
own domain for your app instead of using
the URL that they have here like this
ugly one on render.com
so that's it for this video that's all I
want to show you I wanted to show you
the deploy process and some of the
common issues that you'll run into when
you go to deploy a Django app or really
any other app but this video was
specifically Django so if you have any
questions about anything that I've done
this video feel free to leave a comment
down below if you like this video please
give me a thumbs up and if you are not
yet a subscriber please subscribe to my
channel so thank you for watching and I
will talk to you next time
Ver Más Videos Relacionados
How to deploy a NextJS app on Digital Ocean instead of Vercel
Zabbix 6.0 LTS: Step by step deployment guide with MySQL/PostgreSQL/TimescaleDB
How to Run PostgreSQL in Visual Studio Code
Serverless API with Cloudflare Workers (Hono, D1 & Drizzle ORM)
How to deploy your websites to Cloudflare's Pages Platform for free
Setup Postgres Database - Jupyter Lab and Postgresql
5.0 / 5 (0 votes)