How to Use a Postgres Database in Django

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone its aunty from pretty-pretty here and today's video I'll be showing you how to connect to a Postgres database using Django so this has been something that people been asking me for for a while so I figured I'd cover it now it's pretty simple it's pretty straightforward and as you can see it's using the same way that you use the sequel Lite database the only difference is you change some settings so the first thing you need to have is obviously a Postgres database setup and I have that here through the service called elephant SQL and here's my information for the database so to do this what I need to do is modify the settings in my project so I have the settings file open and I need to scroll down to where it says databases so database is here and as you can see by default it is using sequel i3 and the name of the database is just the path for the project and the name of the database would be dibiase July 3 so if you want to use Postgres this is what you need to modify and the first thing you need to modify is the engine so the engine in this case is sequel i3 but the engine that I want is going to be post Gress key well just like that if you're using my sequel then it would be my sequel like that but I'm not so I'm using PostgreSQL and then the name needs to be changed to be whatever the name of your database is so I'll go back to elephant SQL to see that and the name of my database is this random string so in xdt KNAT so I'll put that here for name and then because this is a remote server and because it's Postgres I need to supply some extra information so there's a user that is involved so user capital R and then there is a password so I'll fill this in in a moment I'm just putting the placeholders here a host with quotes so hosts and then finally the finally there is the port so the port is the port for the server so I can get all this information the username just happens to be the same name as my database the password is going to be this string here so I'll just copy that and put in here the host is also going to come from here stampy DB Elephants equal comm that is the host so whatever the URL is for your particular server and then the port will be 54 32 so I'm getting it from this string here because it's not mentioned here but using the connection string you can tell what the port is it just depends on what you're set up on but 54 32 is a pretty standard port for PostSecret or PostgreSQL so the host could be localhost so instead of a URL it could be localhost if you're running the database server on your local machine but in this case I'm using the elephants equals service so I have the URL for that there so I have a PG admin open and I look at the tables that I have and right now I don't have any tables so when I go back to Django make sure a Settings app is saved and then I can migrate so python managed pie and then migrate and then what this is going to do is it's going to create all of these tables on my Postgres database so I think they're like 10 tables and it should be done in just a moment here ok it's done and as you see on the left-hand side it did not create a sequel like database for me because I am not using sequel Lite my so if I go here to tables and then hit refresh I should see the tables that were created and if I open this up I see the standard Django tables so the auth tables and then the Django tables so if I wanted to create my own table then it would be pretty much the same so basically what I'll do is I'll create a simple app I'll call it simple so say start app and we'll just call this simple and then what I'll do inside of simple well first I'll add the app here so simple so it knows that I'm making changes to the models and simple and then for the models and simple I'll just create a simple model here so I'll call this my simple model it's going to inherit from model stop model and then I'll just put call is going to be models dots char field let's say max length of 10 okay so just like that and then if I make the migration so make migrations and then invalid syntax because I always forget the colon there but if I do that again it should work so it created the migration for me and then if I run migrates it should migrate that table to my Postgres database so if I go here to my viewer again and then refresh it tells me I have 11 tables now and the 11th table is simple underscore my simple model so the simple part comes from the name of the app and then my simple model is the name of the table and of course I don't have anything in it but you can see that it is working on Postgres so everything from this point on is just regular use of models in Django so you know you create the models you queer you from the models and so on so you can access the data that you have in your database it works exactly the same as it does with sequel Lite it's basically the only difference is that you have to change the settings to use a Postgres database instead of using the default sequel Lite database that is there and if you want to learn more about how to use the database in Django meaning how to use the models and how to create different tables and how to query data from those tables you can go to my website pretty pretty calm where I have a free course on working with the database in Django it's about 2 hours long I believe there are like 23 videos you can see all the lectures here that I have so you can just go to my website and sign up for this course for free I'll put a link in the description below if you want to sign up for the course as well but basically this course will get you started on the most important things you need to know when dealing with the database in Django so that's it for this video that's all I wanted to cover it's a pretty simple topic but it is important if you want to use a Postgres database or any other type of database that isn't a sequel Lite database on your local machine so if you have any questions about this you can leave a comment down below and I will take a look and try to answer it the best I can if you like this video please give me a thumbs up and if you haven't subscribed to my channel already please subscribe so thank you for watching and I will talk to you next time
Info
Channel: Pretty Printed
Views: 105,085
Rating: undefined out of 5
Keywords: postgres django, postgresql, django, django database, tutorial, python
Id: t6RbanOhna4
Channel Id: undefined
Length: 7min 6sec (426 seconds)
Published: Thu Mar 22 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.