hey there in this video I'm gonna explain how you set up a PostgreSQL
database with a django docker application the reason I'm making this
video is because somebody on my udemy udemy course asked the question in the
Q and A I think his name was Imran, he asked the question about how to
set up the application that we create in the course with PostgreSQL and in the
course I used vagrant to set up the to set up the development server that we
work on in the course however it's actually quite involved quite an
involved process to set up PostgreSQL in the vagrant you have to create quite a
long bash script and configure all different kinds of things on the
vagrant server it's a lot easier to get this set up if you're using docker to run
your application it's literally just a few changes in the project so in this
video I'm going to go through the steps to convert a existing docker Django
application to use PostgreSQL if you don't have an existing docker
application then that's fine because I'm going to provide one in the link in the
links in this video that you can just clone from github and you can get
started where I am starting from okay so let's begin we're going to start by
heading over to the link to the BYOB profiles REST API docker project which
is a project that I created in a previous YouTube video which I'll also
link in the description of this video but if you head over to this on github
and you basically just want to clone this to your local machine so I'm gonna
clone this down to my demo workspace here get clone and this is basically a
simple Django rest framework application that just has an API that I
create in the Build Your Own Backend Udemy course okay so once it's cloned we're
gonna open this in our Atom editor or whatever text editor you want to use
and then I'm going to show you the changes that we're going to make to the
source code okay so let's just take a quick recap over the code that's in here
so we have the SRC project which has a standard Django application which in it
has is just a simple Django app with a profiles API app that basically is just
a basic REST API using the Django rest framework we also have our docker
compose here which consists of one single service that runs our run server
command for our Django project just to get our code up and running so let's
just go ahead and use docker and just type docker compose up so we're just
going to see what the current state of this is so if we... it may take a bit longer for
you if you haven't already got the images cached on your machine so you
might want to pause the video wait for that docker up or docker compose up command
to complete but once it's complete you should see the URL that we can use
or we actually need to access it on our localhost URL and it's on port 80 and
you can see that the application is installed and running here you can see
the outputs coming in the terminal window that we have here okay so that's
the existing project so now we're going to modify this to add a PostgreSQL
database so I'm going to just do ctrl C here to stop this and the first thing
we're gonna do is we're going to modify our docker compose to pull down a
PostgreSQL image that we're going to use to run alongside our web our web image
that we can basically connect to as our database so at the bottom of
the docker compose file let's create a new service called DB so type DB : and
I'm just going to make this a little bit bigger so it's easier for people to read
increase font size there we go okay so we've got our DB here DB service and
that's going to be adjacent to our web service that's important that there's
the same number of spaces in between the web and the DB and what we're gonna do is
unlike here where we chose to just build from the current
directory because we're building the docker file that's in our project we're
actually going to pull an image from the internet so you can look for the Postgres image
that's in the docker hub and you basically you just want the name which
is simply just postgres but if you go to
hub.docker.com you can search for
all different kinds of images all different kinds of databases if you
wanted to do this with mySQL then you could do this quite easily with
mySQL or MySQL as some like to call it but basically this is just the hub of
all the docker images and we're gonna find out Postgres image and let's just
copy that and let's type image here Postgres okay so what this
will do is it'll basically pull in a DB image running PostgreSQL and it will run
that alongside our container there's one more change that we need to make we need
to make the DB image accessible by our web image or our web service sorry so we
want to make... we want to basically say that this service depends
on this service because this one needs to be running before this will start
otherwise it's gonna error with saying the database cannot be found or
something like that so the way we do that is we add a new item to our web
service and it's called depends underscore on okay and then you
basically just tell it what other services this service depends on and
that way when you run docker compose up it knows that you need to start the DB
service first followed by the web service and it also knows that the web
service should be able to access the DB service
via a TCP connection and basically resolve the hostname so it will
automatically if you're on the web service it will automatically allow it
to connect to the DB service by simply typing the hostname DB okay so now that
we've made that change let's save this file and the next thing we need to do is
we need to add a new dependency to our requirements and the reason we do this
is because we're going to be setting up our Django project to use the PostgreSQL
back-end which requires the um it's called the p-s-y-c-o-p-g-2 libraries so let's type
that p s y c o p g 2 and I have no idea how to pronounce that I'm sure there is
a way but I'm not gonna try it on this video so we're gonna do we're gonna add
this p s y c o p g 2 dependency and we're going to
pin it to the version 2.7.3.2 and you don't need to do this
pinning if you don't want I'm just doing this because it means anyone else that
follows the video there's not gonna be any changes or updates or anything that
makes the steps any different okay so now we can save this file and the next
step we need to do in the final step is we need to modify our settings file
within so it's within SRC profiles project profiles project settings and we
basically want to change this we want to scroll down and find the databases
section here and we want to change this to reflect our new PostgreSQL database
and the way that you do that is I'm just gonna minimize the side window so you
can see the fullscreen there you go okay the way that you do this is we're
just going to change this engine here and we're gonna type PostgreSQL
as our new engine and then we're going to just remove this bit here which
basically sets the name of the old database and we're just going to put in
Postgres and then in the user we're gonna put in Postgres
and in the host we're gonna put in DB if you remember in our docker compose file
we have the depends on that means this hostname will resolve to this service
when it's running so we can just use the word DB there and then the port we're
going to use the default port of 5432 just a default postgres
port okay so now what we need to do is save this file and now we can go ahead
and run our docker compose to bring up our service alright so the first thing
we're going to need to do is we're going to need to run a database migration so
in the previous video I explained how to set this project up and how to do all of
this so basically what you want to do is you just want to type docker compose run
and then the name of the service and then we're going to type Python SRC
slash profiles project slash manage. py and then migrate ok and what this will
do is docker compose will run this service which because it has a
dependency on this service would also run this service and then from the web
service it will call the managed.py script and it will run migrate which
will run the database migration and set up all the tables that are required for
our project in the new PostgreSQL database let's go ahead and hit enter
there and again if you don't already have the data based cached in your
system then this may take a while for this to run because it needs to download
the required images and stuff but as you can see I already have it cached and in
fact I already have ran the migrations because I practiced this before I did
this video so it says that there's no migrations to apply but in your one it
should have a list of migrations that have been applied okay so next we can
we can finally go ahead and run docker compose up and this will start our
docker container which will basically bring up our application and then we can
go and if i refresh this you can see that it still works and if I just do or
it still has the data that I created in my previous
PostgreSQL database but if you want to go ahead and create a new user just call
this mark and then we'll just give it a password awesome one and you can see
that it successfully creates users and it still works just as it did before
except this time it is using the PostgreSQL
database okay so that's how you convert an existing django application that is
using a sequel Lite database to use a PostgreSQL database with docker thank
you so much for watching I hope you find it useful if you have any questions or
comments or anything then please let me know in the comments section of this
video and otherwise I'll see you next time