Installing PostgreSQL Locally to Add Persistence to a Svelte App

Installing PostgreSQL and Initial Setup

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To 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 Fullstack Svelte 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.

This video is available to students only
Unlock This Course

Get unlimited access to Fullstack Svelte, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Fullstack Svelte

Hello, we're back again, and in this lesson we're going to talk about database persistence. Lots of apps need the ability to create, read, update, and delete data, and then to be able to permanently store that data. And this is known as persistence. Persistence is usually achieved by storing files on disk or in a database such as a relational database or a NoSQL database. For our school lunch app, we are using Postgres QL, which is a very tried and true open source relational database used by thousands and thousands of apps and has been around for a long time with lots of success. Now just for comparison purposes, NoSQL databases store JSON directly, and some examples are Mongo and Firebase, and they're pretty easy to get started with, but they have an informal relationship of your data. So it's basically just JSON, and it can be a little bit difficult to mass update your data or query your data or produce reports from your data. So there's a bit of a tradeoff. Relational databases have a formalized representation of the data and have tables with rows and columns. And relational databases are perhaps a little more difficult to get started with, but then they're pretty nice once you're going and you're using your application and trying to administer your application. And we're going to install Postgres locally, but there are cloud providers that offer hosted Postgres, and later on at the end of the course, we'll show you how to do a production deployment to a hosted Postgres as well. But for now we're just going to install it locally. So where we left off with our backend is we're just returning a hard-coded lunchweek list from our lunchweek API. And so this is where Postgres is going to come in. We're going to actually store this data in rows and tables in Postgres. And then our backend will fetch it from the database and then serve it to the front end. And later on we'll do create operations and delete operations and update operations as well. So to get going, head over to enterprisedb.com and find the right installer for your OS. So I'm running the Mac OSX installer. And I've already downloaded it here. So I'm going to go ahead and launch it. Give this a moment to get started. So we're pretty much just going to accept the defaults on this installer. In particular, PG-admin4 is an administration tool that we're going to use to administer the database. So leave that. You can uncheck stack builder if you want, but you may as well keep it. Accepting the defaults. Now we need to create a super user password. So make sure you remember whatever you use here because we'll need it later. Set the default port, default locale, and finish up the installer. Okay. So that's it. Go ahead and hit the finish button there. Okay so part of our installation includes PG-admin. So we're going to go ahead and launch that now. I'm using spotlight search to find it. So PG-admin runs a little web server locally and it's a browser based tool. So the user interface will open here in your browser. And it may prompt you for the password that you use when you installed. So go ahead and put that in. And then over on the left hand side are your servers. Now you'll probably only have a Postgres 13 server there. Just ignore this old Postgres 12 server that I have. And go ahead and click on the 13 server and choose, well now you may, you probably won't be connected to it. So you'll probably need to choose disconnect, or excuse me, choose connect to the server. And then you'll put that password again. Okay so once we've connected to the server you can see that there's one database in there. And it's the default Postgres database. So what we're going to do is create our own school lunch database. So right click on databases, choose create database and we'll give it the name school underscore lunch. So that'll be the name of our database. You can just leave the owner as Postgres and hit save. So that'll create that database. And then we don't have any tables yet. We're going to hold off on those for just a minute. And that's going to be it for this lesson. So we've installed Postgres locally and created the school lunch database. Now we don't have any tables yet. And at this point we could use PG admin to create tables just by writing create table scripts. But that wouldn't be very reproducible. And we ultimately want to be able to deploy our application to production servers. So we'll want our database structure to be reproducible via scripts that are in our project's directory. And so what we're going to do in the next lesson is use next JS and specifically next migrations. And that'll allow us to create database management scripts that will create our tables. And we'll be able to keep those files in our directory for our project. And so later on if we need to create a new database or a database on a different server, we'll have all of those source files to create the exact database that we need. So that's it for this lesson. And we'll look forward to seeing you in the next one. .