Easiest Way To Connect Django To A Postgres Database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right what's going on everybody so in this video i'm going to show you how to create a production level postgres database and how to connect this to your django application in just a few minutes so typically i use aws with an rds instance to host my postgres database and if you've ever used aws you know there's a lot of complexity that goes with it there's a lot you can do with it but oftentimes that creates challenges sometimes you just want to make that connection really quick and just get started so while scrolling on twitter yesterday i heard about railway so i decided to check it out i saw this post clicked on the link and then went through the process and i saw that you can actually host a postgres database so i went through that process and it took me about two to three minutes to create that connection so if you've ever worked with aws you know that it takes a lot longer than that just to set up a database so that kind of blew me away so i decided to write up a quick article and make a video on it so let's just go ahead and get into this i won't go much into railway and their features because i literally just looked it up yesterday and got started i don't know too much about it but i looked into pricing and so on and it seems like a really good platform so i'll tell you more about it later once i learn more but this process was cool so this article is linked up in the video description i'm using dev.2 this time instead of medium so let's go ahead and get into it what we're going to do here is set up a quick django application we're just going to run some pip installs and then we're going to create a database with railway and make that connection so let's go ahead and get into it the first thing we want to do is install django i set up a quick virtual environment you can do it or don't do it i just wanted to make sure to separate everything so we'll install django and then we're going to install psycho pg2 if that's the right way to say it that's also in the article and basically it's just a database adapter to postgres so we need this in order to use postgres with django so we're just going to do pip install psycho pg2 okay i'm actually spelling that wrong so give me a second it's going to screw up the speed of this video so let's go ahead and do that so psycho pg2 yeah that looks about correct okay so now let's go ahead and create a django application let me remove this right here i'll zoom in and we're just gonna do django dash admin start project and we'll just do railway underscore django so just create the app cd into that okay and there we go so i'm quickly just going to open up a vs code instance um give me a second here create a new window i just need to close out another tab here because it's on the other screen here okay so go ahead and open up the folder and i have a bunch of crap on my desktop so let me just try to find this i'm always testing things out so railway django and here is our application so we're going to stick to the command prompt instead of using this terminal so in order to connect django to postgres we're just going to go ahead and go into installed not installed apps but into databases here and we're going to change this from the built-in sqlite3 database to postgres so the first thing we'll do is go ahead and grab this and maybe i'll zoom in i don't know how well you can see that we'll copy that and we'll modify this and then we're just going to go ahead and add in the name user password and port and we're just going to leave these empty strings so this is what we need to make that connection this is how django knows about the database so that's the first part we have the jgo app ready and what we're going to do is go ahead and go to railway app or railway.app so let's go ahead and open this up go ahead and create a new account i actually just created an account already so once you have that ready you don't need a credit card on file so it's really fast to set up go ahead and go into dashboard and create a new project and select provision postgres database here so this is going to take a second it's much faster than aws usually that takes like 15 minutes just to get that set up so we'll just see how long this takes there we go it's up and ready and this is going to give us some default information we're not going to go into setting up things like the username and password we're just going to go with all the defaults because we're going for speed here so here's our database we'll go ahead and click this here and if i go to connect this is that connection string so it kind of looks like gibberish i'll actually delete this after but here is going to be the password so if i click hide and show let me actually move this out of the way so if i click show here we can actually see the details so here's my password and then we have different attributes here that we're going to get into so in order to actually make sense of this we're going to go into variables here and if we click on database url this tells us where the user is password and so on so it's going to give us everything that's built into that connection string so we're just going to go ahead and take each value here so this is going to be my database name here so that's going to be railway so i'm sure you can modify that later i'm very confident of that but i just haven't done it so i won't show that right now so that's our name right there then we have our host here so that's going to be that url this is going to be the host in this section we're just going to copy that then we want a password so we'll copy the password we won't worry about environment variables or anything like that and we want a user so we'll go into pg user copy that and let's bring this in so we have the user and then the port number so that's the last part in this process so we'll go to pg port it's 6725 copy to clipboard bring this in and that's it that's the connection it was that fast so in order to actually make this work first of all let's look at our database we have no tables so if we look at data we can actually write sql queries here we see all this information about our database if we go into data we can actually create tables here but with django we don't want to do this we just want to go ahead and run some migrations so we have a fresh project up and running so what i'm going to do is go ahead and run python manage dot py migrate i've got the command and that should do this and it looks like i have a misspell so python manage dot py migrate okay so that should migrate the database there we go we're going through the default migration so this will create things like the sessions tables the users and so on and as i look at the database here in the background i can actually see everything get created so if you look here we can go into auth users we see our table we see all the attributes here let's actually create a super user so we'll do python manage dot py create super user and we'll just go ahead and go through the defaults dennis dennis at email.com and create a password all right our user is created so if i refresh it here we go we see our database table so if you ever used aws you know that this process takes a lot longer that was really fast we created that connection i might come out with more videos on railway and how to do things like customize things go through the payment plans and so on but that was it i just want to keep this video nice and short go ahead and give this a test i'm really excited about railway and yeah go ahead and test it out
Info
Channel: Dennis Ivy
Views: 56,759
Rating: undefined out of 5
Keywords: Programming, Software Developer, Dennis Ivy, Dennis Ivanov
Id: HEV1PWycOuQ
Channel Id: undefined
Length: 7min 27sec (447 seconds)
Published: Thu Jul 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.