>> In our last video,
we showed how we can deploy
our Django application
up into Microsoft Azure.
There's one thing I skimmed
over towards the end
and mentioned that the database,
why we deployed with SQLite,
that's okay just to get us started,
it's really not a very scalable option.
Actually every time you deploy
your application back into Azure,
it's going to override that file.
SQLite really mostly used
as a testing database.
It can be used in production environments,
but it's quite tricky to get right.
A better option is actually to deploy
database server and I mentioned
that Postgres is a popular database server
that is offered in Microsoft Azure.
In this next video,
I'm going to show you
how to change your application
to use a full database server
and how to deploy one of those
in your Azure environment.
First things first,
we're going to go to
the Azure tab in VS Code,
and down at the bottom,
there should be a section called databases.
Same as before, you need
to make sure you've signed into
Azure first with your Azure account.
Just as we created a new application
here through the App Service component,
we would create a new component here for databases,
through the databases pane.
If you click on "Create Server" here
and then pick Postgres,
we need to provide a unique name
for our Postgres server
and this will also form what we connect
to when we've deployed.
I'm going to use ants really Cloud DB,
and then "Enter."
We need a username that we're going to
connect to the database with this
will also form the administrator username,
and typically you'd have one user account
for the admin and then one for the web application.
Just in case the web application
goes a bit crazy or you accidentally
put a security hole in.
But for now, I'm just going
to create one username.
We'll keep it simple
and I'm going to call that Django,
so that the username will be Django,
and then I'm just going to set a password here.
It's going to ask me if I want to add
a firewall rule to allow my local computer
to be able to connect to the database.
You can allow that if you want.
I'll show you how to do the migration
in a second and it's important,
I think when we do that step that you can
connect locally from your computer.
I'm going to skip it for now,
and I'll show you how to
do that through the portal.
Then for the penultimate step,
you need to pick which resource group
you want to put this database server in.
Pick the same one that you set
your Django application into,
which in my case was Demo 2.
Then lastly,
pick which region you want to go to.
This should be the same region that you
deployed your web application to.
You want to keep the communication
between those two things as fast as possible,
and I deployed into West US
while I'll pick that one.
It's now going to go and actually
provision a Postgres server for me,
and it's going to keep me updated
about all the steps.
That probably takes about
five minutes to complete.
In the meantime, we can go back
and look at our settings because we're
going to need to make some changes
before we redeploy into production.
In the project folder,
there should be the file settings.py.
When we looked at this before
we deployed into production,
we changed a couple of things
like we turned debug mode off,
for example, and we also changed
the allowed hosts and we set
this secret key at the top as well.
There's something else
that's important next,
which is the database configurations.
You'll see that here,
and the default is to use the SQLite engine,
which is this file that got created
automatically called db.sqlite3.
We're going to need to change
this configuration to instead
connect into Postgres.
We're going to basically,
remove this section altogether,
is really important to note
that if you've put anything
in the database at this point,
like the locations for example,
or destinations, things like that.
Then if you just change this configuration
when you provision the new database,
that data is not going to be migrated across.
Next, if you got up to the Azure portal
and then you look for the Azure database
for PostgreSQL and pick the second option,
Azure database for Postgres servers.
Then you should see the database server
that you just provision,
so ours was called ants ready Cloud DB.
We can click on that,
and it's going to give us some
information that we need to tell Django,
so it knows where to connect to.
The first thing I need to do though,
is go down to Connection Security
and I need to open up our database server
so that both the production web app
and also my computer can connect into it.
I'm going click this button here,
"Add client IP address".
It knows that's my IP,
and then I'm also going to tick
this box which says allow access to
Azure services so that my Azure Web App
will be able to connect into the database.
Once I've done that, I can do "Save".
Then once that's finished,
both my local machine
and our web application
will be able to connect to
the new Postgres database server.
Going back to overview,
we'll need to keep that open
whilst we configure our application
in the background.
If you go back into settings.py,
if you click on that link there,
it'll give you some examples
of how to configure Postgres.
We can copy this configuration here
and just use that as a template
for us just to get started with,
just to make things a bit easier.
The the name is the name of the database.
In this case, we can just call it Django.
The user was the username
that we specified when
we provisioned the account.
If you go back into the portal,
it'll remind you what that is.
So we can copy that to the clipboard
and then just paste it in here.
The password.
Now, I don't particularly want to write
my password in plain text in here
so that everybody can read it.
We're actually going to do
something slightly different
and we're going to load that
from the environment variables.
I'll show in a second
how to set that in there.
OS, I haven't imported that yet in this file,
so I can click on "Quick Fix",
and it will import that at the top
of the file for me.
So os.getenv for get the environment
variable for the password
and set that as the password.
Next we have the host,
which will be the server
that we're connecting into.
Again, if you go back into the portal,
you'll see the server name here,
we can copy that across,
and then paste it into our host environment.
You might want to make this
an environment variable as well.
Just in case maybe it changes
or you want to modify things,
we'll have different database servers.
But for now, I'm just going to leave it like that.
There's also one extra setting that's missing,
which isn't in the example,
and that is that Postgres on Azure sets SSL,
which is an encryption mechanism
to be required by default.
You can turn that off.
But I don't recommend doing so,
because it is more secure to have it on.
What you'll need to do is add
another section called options,
which is a dictionary key is SSL mode
and the value you want is require.
That will make sure that
when it tries to connect,
it will use SSL in the connection.
You set the database name as Django.
But the Django database doesn't yet exist
and the migrate command for Django
won't create that for us.
Luckily, we can do it through
the VS Code extension.
If you open up the Azure tab again,
go to databases,
find the database server
that you've provisioned,
you might have to click "Refresh"
to see it there.
Expand that and you'll see
the Postgres database,
which is the default one.
Leave that well alone it has mostly
configuration settings for Postgres.
But if you right-click on the "Database Instance"
and then do create database,
we can then type Django and it will
create a Django database for us.
That means when we run the migration command,
we don't need to worry about that
database existing beforehand.
Once you're using Postgres
as the database server,
there's one extra dependency
that you need to add to both
your virtual environment locally
and the one on the web app.
That's something called Psycho PG,
which is the Postgres client for Python.
If you open up the requirements.txt file
and add psychopg2-binary as the requirement.
You'll also want to install that locally,
because we're going to test this in a second,
so pip install psychopg2-binary,
and then it will pull that down
and install that for you locally as well.
Once that's all configured,
we can deploy the base database.
This is all the tables, and the structure,
and stuff by running the migrate command, likely.
Because we've allowed the firewall
for our machine to connect
into the database server,
we can run that migrate command here
before we deploy the application
back up into the web application.
If you open up a terminal,
we're going to need to set
this environment variable first.
A couple of ways you can do this,
but one probably the easiest is that
you can do that on the command line.
For Mac and Linux,
you would use export the name
of the environment variable
and then the value for Windows
you do set DB_PASSWORD,
and then the value after that.
I'm going to do that in locally,
I'm on Mac, so I'm going to use
the export command and do my password.
Python managed.py migrate.
It will connect to our new database server,
it'll provision all of the tables for us
and all the structure,
so it'll tell us that it's running
all the migrations ready to go.
Then once that migration is completed,
we can then go ahead and push
our application up onto App Services.
Before we do that,
we need to make sure
that environment variable
with the password in it is going to exist
on our application service instance.
Again, go back to the Azure tab,
go to App Services and locate the apps
that you had deployed before,
so mine was aunt space tours,
go to application settings,
right-click on that and do "Add New Setting."
That's going to be DB_PASSWORD as the name,
and then the value will be whatever
you configured as the password
and just hit "Enter",
and that will synchronize
with the remote server automatically,
and then once you're ready to go,
we can click on 'Deploy to Web App."
It's going to warn us there's going to
overwrite what is there already,
and the deployment is going to run,
so we can keep an eye on that like we did before,
by just watching the output Window.
Then our website is up and running
with the full database server.
Now, there is one last thing we need
to keep in mind and that is
if you go to the "Admin Page",
it's going to ask you to login with
the super user name and password.
But because the super user record is in the database,
then it's still going to be in that
SQLite database that you created
at the beginning of this project,
and it's not going to exist on the Postgres
database that you've created.
You will need to go back into your terminal
and do python managed.py create super user,
and then provide the username,
your email address, and any password
that you want to set for the super user accounts,
and then once you've done that,
when you go back,
that should just let you log in there.
Now, if you want to go ahead,
and add the cruises, and the destinations,
and stuff back into the database.
Then you can do that through the Admin Portal.
Congratulations, your web application
is now running in production,
you've got your database deployed,
you've got database server running,
and Django is all set up and ready to go.
[MUSIC]