Postgres Database + AWS RDS | Django (3.0) Crash Course Tutorials (pt 21)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so in this video I'm going to show you how we can connect a post crest database to our Django project so what we're gonna do is actually download Postgres and PG admin which is just an interface that allows us to work with Postgres and we'll create a database and then connect it to this app but in the second half of the video what we're gonna do is actually put this database on AWS so we'll actually create it on Amazon Web Services and then connect it to our local projects so the project will be local we're not gonna launch it just yet but the database will be live and something that we can use once we put this project on a server so I'm not gonna spend too much time on Postgres and PG admin but I will go through the actual process of installation and I'll just make some comments here and there once we get things set up so go ahead and open up your browser and just do a search for Postgres and mine's gonna probably autocomplete it but I'll just go ahead and do the Google search and from here we can just do download and select the option that works for you I'm on Windows so we're gonna click that right there and we're gonna click download installer so this is gonna give us the installer to actually install the Postgres database so I want a Windows again and I just want the latest version so select what you need here but this will probably work if you're on Windows and this is gonna give us a this is gonna take a second so just give it a couple minutes here to download the Installer and I'll probably just fast-forward through this but once it's ready we'll go ahead and start working with that okay so my installer just downloaded so we can just open that up here and probably close out this browser and just go ahead and click yes here so I already have Postgres installed so my process might look a little bit different from yours but for the most part it's gonna be the same and one thing you need to be aware of is that you're gonna be prompted for a password through this process if this is your first time so make sure you save that password because that is gonna be the password that we need to log in to PG admin so just make sure you remember that password and save it somewhere and I'll just go ahead and let this install run and I'll probably just fast forward or just cut this part but I'll get back to it once this install is complete okay so let's go ahead and just finish all this up and for the stack builder we're going to do PostgreSQL and we're gonna do the database server and we're gonna do a post rest 10.1 and just finish up this process so we'll skip the installation for now and now what we need to do is actually download PG admin so go ahead and open up your browser again and just do a search for PG admin and this is the website so this is the interface that's gonna allow us to actually work with our Postgres database so go ahead and download find the one that works for you I'm on Windows and we're gonna do the latest version and we're gonna do this middle one here so it's gonna click that and give this also a minute to just download and I'll probably cut this too okay so now that it's downloaded we can open this up and it's actually gonna open this up in our browser so give this a minute yes and I'll close this out and we'll just finish up this installation process except and just go through all the defaults again and when we hit finish it should just launch it in our browser so we'll just open this up and actually go ahead and create our first database okay so it actually opened it up on my other screen so it's just loading here and again it is gonna be a local server but in the browser and this is the password that we needed to remember when we first created our Postgres installation so go ahead and type that in and this should log us into PG admin and from here what we're gonna do is we're gonna create a server group so this is gonna be just a group that holds our databases so to do that we're gonna right-click and we're gonna create our server group and I'm just gonna call this one demo and we'll save that and within our server group we're gonna create an actual server and I'm gonna call this demo server and we need to create a connection so the connection right now is gonna be localhost and our port number in your case you should probably leave it at 5 4 3 2 unless you changed that but because I had to install over my original 1 - 5 4 3 2 4 I believe so make sure you're just aligned with whatever you have and specify that password again that password that we created on that installation so just make sure that aligns and I'll see if this one works for me okay so that was created perfect and here is our server group our server itself and it already created a default database for us so what I'm gonna do is actually create a completely new database within our server so to create a new database we just need to give it a name and I'm gonna call this one demo underscore test and I believe that's the only parameters we need for now yeah so we just need to give it a name and once I save that here's our database we can make our connection and if you go to schema public and then tables right now we have no tables because we didn't migrate anything or we didn't actually create anything manually so if I right click we can create some but we are using Django so we're just going to run migrate and it's going to make all this for us just like it did in our SQLite database and before we go ahead and start connecting our database to you we need to run one quick pip install and I already have this but we need a pip install psycho PG - I believe that's how it's pronounced and this is a database adapter that allows Postgres to talk to a Python any kind of Python program so we're gonna run this pip install so I'm gonna just copy that cuz it's kind of weird to spell and in my command prompt I'm gonna run pip install and I'm just gonna paste that in so psycho PG - and I already have it so it's gonna tell me my requirements are filled but just go ahead and let that run and in here now we need to change up a few things so we're gonna change this from SQLite to Postgres so this is gonna be post Gress ql all lowercase and the name is gonna be the name of our new database so we're we're gonna find this is actually within PG admin is just this database so demo underscore test so that's the new name and I'll just specify that so demo underscore test and now we need a user so the user is also going to be taking all from PG admin and I'll just show you where we can get all of these in a second so I'll go ahead and just list them so the default user for now should just be Postgres if you just went through the default installation and then the password so password is give me the password we specified and I'll leave that as an empty string for now and the the host is gonna be localhost so host is local host and the last thing we need is the port number so the port number yours should be if you went through the default should just be 5 4 3 2 but mine is 5 4 3 2 4 just because I made one configuration so make sure you don't copy mine directly do what you have here and to find all this information it's just right here if you go to your server and go to properties it was this setup right here so the host was localhost that's my port number and the username so if you don't know what yours is just go back to your server and find it from there so I'm gonna go ahead and just fill in my password and we'll set that and we can run a migration now so let's go ahead and open up our server and do Python and the reason we're able to just run this migration is because I already have a database so we don't need to run the make migrations command and we'll just do my grace so manage py migrate and if I go back to my database now and in here if I go back to tables and hit refresh we should see all of our tables now within our Postgres database so there we have customer order product our user and we can actually view these tables by going here and just doing view all rows so right now because it's a new database we don't have anything so we need to create a new super user new customers and so on but that finishes up the process of actually creating Postgres database and connecting it to Django so it's as simple as adding a few more parameters and just running that migration so now what we're gonna do is actually create database on AWS a Postgres database and we'll connect it so the steps aren't that much different other than the fact that the database is now hosted elsewhere so the connections are going to be a little bit different okay so before we create a database on AWS you're gonna need to create an AWS account so if you just do a search for AWS and I should make that a Google search it'll be here so aws.amazon.com and i'm already logged in so once you create an account you are gonna need to put a credit card on file we're gonna work with a free tier so don't worry it's not gonna get charged unless you actually upgrade or go over certain usage so for this video just make sure you add it and just get started that way but you won't need to spend anything just yes so once you create the account go ahead and sign in and we're gonna work with RDS so it's a relational database service and this is where we can actually create different types of databases so if you log here you're gonna see a few options and the one we're gonna select is gonna be Postgres so in RDS we're gonna click create database and we're gonna work with Postgres but you can use whichever one you want and the process is pretty much the same for all of them from my experience so we're gonna select free tier and we're gonna leave all the defaults here for now but I'm actually gonna set a username so I'm gonna call mine Dennis IV and again I need to remember this information when for when I connect to my PG admin account so I'm gonna go ahead and just create some kind of password so I'm gonna do cuz I just type something in and I'll confirm that and I actually think I misspelled the first time so just double check that okay so we created our password and as we scrolled down just trying to make sure I don't miss any steps here there's something in this tab right here that we need to make sure we set so in publicly accessible make sure you set this to yes so this basically means that we can work with this database outside of AWS so if we leave this at no we won't be able to log in we're gonna get a connection error in PG admin so make sure that's set to yes if you didn't do this you can actually change this from the database which I'll show you we can update this later but leave that there and we're gonna work with all the defaults so VPC we're going to leave that to default and the port number is gonna be 5 4 3 2 so we are gonna need to remember this port number for when we connect and we're gonna work with just password authentication so we're not gonna setup I am user or anything like that and let's see initial database name I'll just call this demo underscore 1 and so far all the defaults look good so let's go ahead and just create and this is gonna take a couple minutes actually so I've seen this take up to about five minutes so just let this run and I'll get back to it once it's ready okay so this one took about eight minutes to complete but now that we have our database we can access it by clicking databases here and within here there's a few things I want to show you so that modification of that access level so whether it's publicly accessible or not can be changed right here so while this reloads here so if we accidentally set that to no we can go ahead and change it right here and go ahead and save that so that's are you able to update that so let's go ahead and open this up again and the next thing I wanted to mention here is that once this is on a server we're actually going to need to change up something with our parameter groups so this is basically a way of allowing a certain connection to our database so right now because we're on a local server we don't need to change this but we do need to later on set the inbound method and I will address this once we're putting this up on a server so now that I got that out of the way what we need to work with it's going to be this endpoint and this is basically like a like a URL path to where our database is stored so if I copy this and if I open up PG admin so let's go ahead and close this out and let's create a new database so if I go ahead and actual just do go under demo under this server let's go ahead and create a new server and this is going to be live BB's and for the connection instead of localhost we're going to pass in that URL parameter so this endpoint right here so it's just a database endpoint we're going to pass that in and the port number if you want to confirm that is specified right here so pore is 5 4 3 2 and I'm gonna pass in the username which I set to Dennis IV and the password I'll just go ahead and type mine in so the password I set on AWS when I was creating that and if I hit save this should connect me to the database ok so perfect the connection is made if I look here live DBS under database it's demo1 so it creates a couple of other databases with it but this is the one we want to work with so if I open up the database and go into tables we currently have no tables there but once I make the connection in Postgres I can actually start working with this so let's go ahead and change this up so the host right here is going to be that URL endpoint the port number is gonna be 5 4 3 2 and I'm gonna give it the new password so I just gave it a random password and I'm gonna delete this can't account anyway so that's not gonna be an issue and I'm gonna set the username and the name of the database which I call that I already forgot but where did I put that name actually it's in PG admin that'll be faster to find it that way so I just called it demo underscore 1 so let's go ahead and do that demo underscore 1 and if I save that I'm gonna need to run a migration again so we're just gonna do Python manage that py migrate and should create all those tables on our database okay so that creates all the tables if I open a PG admin and refresh that we should see our new tables there and the cool thing about this is that this database is now live so if I want to access this database from another computer let's say I'm working a remote somewhere I can actually log in with these credentials and access this and when I put this up on our server this database is a real database it's fully accessible from anywhere now so that's a really cool thing to accomplish that AWS makes very easy for us so right now everything's on a live database but on a local project so in the next video what I'm gonna do is actually set up as three buckets on AWS which basically is a way for us to store these static files so I'm actually not gonna host our static files or images in here because that's just gonna be a lot for a project to handle these will actually be stored on AWS so we're gonna do that too so that's also gonna be in a local project with static files on a live server and then finally we'll just close this out by putting this project up on a real server so it's publicly accessible from anywhere and we'll connect a real domain to this
Info
Channel: Dennis Ivy
Views: 68,444
Rating: undefined out of 5
Keywords: Programming, Software Developer, Dennis Ivy, Dennis Ivanov, Django, Python, Web development, Postgres, PostgreSQL, AWS, Amazon Web Services, RDS, Intergration, Psycopg2
Id: 3HPq12w-dww
Channel Id: undefined
Length: 17min 8sec (1028 seconds)
Published: Tue Jan 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.