
Build a Fullstack App with Next.js, v2 Setup a Postgres Database on Railway
A second and third version of this course released using Next 13+, but this course is great if you want to see another example fullstack project. We now recommend you take the Build an AI-Powered Fullstack Next.js App, v3 course.
Transcript from the "Setup a Postgres Database on Railway" Lesson
[00:00:00]
>> So being a full stack app I think the biggest thing that we have to think about is a database and where we're gonna save our data, where we're gonna put the data. So we're going to actually use a real database here so if you don't have any experience with databases or anything like that that's totally fine.
[00:00:17] We're gonna walk through how to set this stuff up. And it's gonna be abstracted away pretty well for us here with the origin that we're going to be using. So you don't need to be a database expert to get the best of this. So it's all good. So to set up the database, we're going to use Postgres.
[00:00:34] The reason it's in Postgres because I think it's just the most widely accepted standard database out there. I've been using it for a while now, I usually use Mongo a lot, but I'm lately just using Postgres because it's, why not switch it up. I feel like I was just kind of capped out on Mongo at this point.
[00:00:52] But yeah, Postgres is really cool. But yeah, we need a Postgres database to be able to work on this app. There's two ways I recommend doing this. One, if you already have Postgres, Postgres on your computer, just start the server. You should be good. If you don't have Postgres on your computer, you can download it on computer if you want to just by going to the post Postgres website.
[00:01:10] The other way is you can just get a hosted one and use it locally. So now that Heroku is gone, and you can't get a free hosted Heroku free hosted Postgres database. I found that you can actually get one on railway. So, if you go to railway dot app, and you make a new project, sign in, whatever, click on new project, and if you type in, what can we help with if you just type in Postgres, you can see over here it says provision, provision PostgreSQL And click that.
[00:01:50] And it's gonna spin up a Postgres database. It's going to do the best that it can and once it's done you can click on settings or, I'm sorry, not settings. You can click on the actual database and then you can click on connect and then here where it says Postgres connection URL.
[00:02:15] You can just copy that. That's the URL that you want. So let's have that URL ready if you're using railway. But if you already have postures on your computer, you can use that. I'm using the one on my computer because I never turn it off. It's always. But either is fine.
[00:02:43] Any questions on that? I think it's like free up till point. You get, like, some hours. It'll be more than what you need for this course, but it's not going to last there forever. Like, maybe that was Heroku's downfall, is that they just they were too generous. So, we're always like we're gonna cut you off if you don't use it.
[00:03:06] Okay? So we want to do is once you have that URL, we need to add that to our env file. So what we can do is if you go to your repo, you need to add a file called dot env on the route like this and inside of here you need to add an environment variable call it database, underscore URL like that And you need to set it to your database.
[00:03:36] So if you have the was it railway, where is it at, if you had the railway one you can copy railway and it will look at something like this or you can paste that in there. And yeah, you can just you get to save that and that should be fine.
[00:03:59] You can add I think you can add something like schema public here. I think that does something with the ORM that we're gonna use that makes it does introspection, but it's not necessary. What I'm just gonna put it there, I always have it there. Okay, and I think you can change this name here for a different database name if you want.
[00:04:22] Yes.
>> Thoughts on render versus railway?
>> I have yet to deploy anything on railway, so I can't speak to it other than just using their databases. But I can talk about render and, render feels like Heroku [LAUGH]. It feels just like Heroku, you could take that as good or bad if you'd liked Heroku.
[00:04:44] I liked Heroku, I thought was pretty cool but railway seems like a lot of potential. I like their develop, I like their dev experience from at least the docs that I've read and the videos that I've watched but I haven't used it so I can't really give any opinions on it so.
[00:04:58] I don't know if someone has used I'd love to hear your thoughts, but it looks cool okay, good to go there all right Next thing we want to do is we want to initialize our ORM. Which is basically if you don't know what ORM is, you can think of is like, it's like an SDK for your database.
[00:05:28] That's, basically the answer. They should just call it that. I don't know why it's called ORM anymore. It should just be like, database SDK. That's literally what it is. We're using one called Prisma. Prisma is like kind of the standard at this point when it comes to interacting with databases, at least in my opinion.
[00:05:43] It's database agnostic. It works on every database. It has its own schema, syntax that's really easy to use. It's pretty cool. So to do that, you can go on your terminal and you can just type in NPX PRIs. I'm in. And you can see right now Prisma is like, we tried to add a database URL but it already existed.
[00:06:08] Actually, if you wanna see how that works, I can, well, you could delete the env file if you wanna see what it generates, but you already added it so you don't need to. But it's basically the same, you got to set the database URL and all this other stuff.
[00:06:23] And if you go and look, you'll see that it added a Prisma folder to your repo. To open up Prisma folder, you'll see there's a schema in there. And that scheme has these two things. It's got this generated client and it's got this data source generated client is saying, so Prisma's SDK for your database is generated.
[00:06:45] So the SDK that you have is different than the one that I'll have. It gets generated on a command because it's type safe. It's the types are generated based off the schema that you write in here. So this is just saying, here is the client file that I want you to use to generate my schema, which is one that they made.
[00:07:02] So this is suggesting, just saying that you could probably make your own if you want it to. I don't know why you would but you probably could. And then data source db is just saying what database we want, defaults to Postgres, and then what environment variable or URL does it point to.
[00:07:16] And there we go, we got that.