Connecting a Django App to a PostgreSQL Database
Let's try to connect to the database
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the Serverless Django with Zappa course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Serverless Django with Zappa, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

[00:00 - 00:09] In this lesson, we're going to test the connection to the database we just created. Before we do that, we need to install some packages on our Zappa environment.
[00:10 - 01:16] First and foremost, we need a database connectivity module. So I'm going to install that right here. It's called "Psychop G2" and I could install this. It will download the source code and compile it, but I'm going to take a shortcut and download a binary version of this package so that I don't have to compile it here. Next, we're going to configure our Django settings. In order to do this, we need the host name of our database. So here I am in the Amazon console . I can see the endpoint is right here. So I'm going to copy this and notice it's on the standard port and that's going to be important later. All right, so if you remember, when we first started this process, we commented out our database section of the Django settings because the AWS Lambda environment is not compatible with SQLite out of the box. So what we're going to do is replace this whole section.
[01:17 - 03:35] Let me just delete this section here. Replace this whole section here with new settings. So we change our host to be what we copied from the Amazon console. The engine is Postgres, the user is Postgres and you need to put your password here. And of course, the name of the database we created is Zappa course DB. So I'm going to save that and then switch over to my console. Okay, here we are at the console. We're going to run some pretty standard Django management commands. So first what I'm going to do is I'm going to run inspect DB. Now what inspect DB does is it connects to the database that's configured attempts to extract the schema and translate that into Django models. Now we know our database is empty. So it 's not going to generate any models for us because there's no schema to speak of. However, we 're just using inspect DB to verify that we can actually connect to the RDS instance that we created. So let's go ahead and do that now. Okay, it looks like my connection timed out. It's asking me if the server is actually running because it cannot connect to my RDS instance. We forgot a step. Well, I forgot a step to tell you guys, let's go back to the Amazon console. And we need to scroll down to security. And you'll see there's a VPC security groups here, even though we were able to mark this RDS instance as public, it's still not quite public yet. The default configuration for most Amazon accounts is to make sure you can't shoot yourself in the foot. So by default, there is no inbound traffic. We're going to change that now. So we click on the zoom in here. We click on the security group. And we click on inbound rules.
[03:36 - 04:03] You'll notice that it's by default traffic between resources with the security group can talk to each other. We need to add a new rule. So we're going to hit add rule here. And it's going to be Postgres. All right. And that looks right. We can set the custom, the source from anywhere. And then we hit save.
[04:04 - 04:25] Okay. Now we should be able to go back to our console here. And let's try this again. And that now is working. So there are numerous other types of errors that you may encounter.
[04:26 - 04:55] When you're trying this inspect DB, this is just one of them. It's pretty good about telling you if you've mistyped it, if you mistyped the hostname, or if you mistyped your password, it generally gives you a pretty good idea of what's going on. What we've been doing so far, we have been running the inspect DB command from our local development system, which is nice that we can connect from our local system to the database.
[04:56 - 05:48] But we also want to check to see if our AWS Lambda version of our code can also connect to the database. So first we need to push up the updated settings file to the cloud. And once that's done, and we've pushed up the latest settings to the cloud, we can actually run remote commands from there and have our AWS Lambda version of our project connect to the database as well. Okay, that's finished. So you may be wondering, how do you run remote commands in the AWS Lambda cloud? That's where the Zappa utility comes to the rescue. We can actually use Zappa to run management commands remotely. So let's try that now.
[05:49 - 06:52] We're using the Zappa managed command, which effectively is the same as the local managed command, but it's going to be running in the cloud. We also have to specify which environment we want the command to be run. And then we actually provided the command itself . And let's go ahead and run this now. Zappa is going to connect to the AWS Lamb da environment and invoke your project in the cloud, run the command, package up the output and send it back to you to view here. And this is what it looks like. You can see at the beginning, tells you the start and a specific request ID. And then it has some debug messages, telling you what your managed command is. And then it's giving you the output. If you notice, this is the same output that we had from running it locally. And then it lets you know it finishes with the request.
[06:53 - 07:36] Some interesting things to note here is to tell you how much time the whole process took in terms of duration and how much time are you build for. In this case, the managed command took 48 milliseconds and it also gives me some information about what the memory size was allocated for this particular AWS Lambda instance and how much memory was used just for this command. But we've established that we can connect to the database and we've established that the AWS Lambda version of our project can connect to the database. So in the next lesson, we 're going to start making Django a little more interesting by adding some models.