Hey guys.
So we're now finally ready to start learning about Postgres.
This is going to be our Database Management System that we're going to use to be able to persist our
data on all of our web apps.
As I mentioned before, the reason why we're choosing to use Postgres is because it is widely used by
many of the top companies that you would have heard of, like, Apple, or Twitch, or Instagram, and even
NASA
uses Postgres to manage their data.
As you can see, for our purposes, it's clearly going to be more than enough.
Postgres themselves say that they are, "The World's Most Advanced Open Source Relational Database."
For us, what that means is it's widely used in the industry, as we already saw, and it's also free
and open source.
So we don't have to pay a single cent to use this powerful technology.
But in addition, there's also great career opportunities after you learn Postgres because it's used
so widely in a lot of companies and also in a lot of industries.
Finally, it has great community support because lots of people use this.
As I showed you earlier, it's one of the top most used Database Management Systems by professional
developers.
So that means if you do get stuck, there's plenty of help available because there's lots of people
familiar with this technology.
So how are we going to use Postgres?
Well, we've got our client, as we've seen our front-end of the website that is going to hit up our
server-side, which is going to be the part that's going to handle the requests from the client,
and in order to handle those requests, there is business logic in the app part of our website.
The app.js or the index.js that handles the requests, figure out what it needs to do,
and that part is what's going to talk to our database.
Our database here is responsible for storing things such as user details, user emails, passwords,
or if you have a blog, maybe it would be storing the posts and other types of data.
If you have a to do list, it will be the to do list items.
If you have a social network, it will be the likes and so on and so forth.
You get what I mean.
All of the data that needs to be stored permanently to be retrieved by the app, depending on whichever
conditions.
For example, if somebody wants to pull up a blog post from November 2021, then they're going to hit up
the database, grab that piece of data, and the app knows how to send it back via the correct response
codes, and to format it for the client to be able to display it.
We've already checked off this, this, and this, and this is the part that we are focusing on right
now in this module.
The code that's required to use Postgres is also incredibly simple, because there are special packages
set up to use Postgres with Node.
You can see here we're using a package called pg, which is an npm package and enables us to set up
and get running with Postgres in our Node backend relatively easily.
So let's run through this code just so you can get an overview of how you might interact with Postgres
from your Node backend.
The first thing we do is we create a const called db, and this is going to contain all of the details
that we need to connect to our Postgres database.
You can imagine the Postgres database as a server of its own.
Previously we saw that we had a server for an API backend.
We had another server for a website that was going to use the API backend, and we had two servers running
simultaneously on localhost:3000, localhost:4000.
Well, now in this case you can imagine your database running on its own database server.
And this can be local, as we'll be in our case running Postgres on our own computer,
or it can be hosted on the internet, in the cloud, in a remote location.
But either way, in order to connect to the database, we need some details to verify ourselves.
The username, the host, the database name, the password for the database, and the port on your local
computer where that database server is running.
We're going to go through these parts later when we actually create our database in Postgres.
But just know that these are like the username and the password to be able to authenticate yourself
with the Postgres database, and also connect to the correct database that you actually want to store
your data to.
Now, once we've added all of those details into our const, then we can use the connect() method that
comes from the Postgres package to connect to our database, and then we can use db.query.
We're going to be using db.query a lot because it is where most of our work is done.
And inside this method what we can do is we can write SQL code.
So notice how this is being entered as a string,
but what's in between the double quotes is what we're already familiar with which is SQL,
so Structured Query Language.
And we already saw how we can use this
and also what maybe this line of code means, "Select everything from the users table."
So essentially we're going to get all of the records from the users table.
If there were any errors then we can handle that,
otherwise we're going to console.log() and output all of the rows.
So all of the users from this user table.
And then finally we close our connection with db.end,
and that is all it takes to start using Postgres with our Node backend.
We're going to go into this in detail and we're going to be doing this many, many times.
Don't worry, this is just an introduction for you to get a taste of how we can use Postgres using our
Node and Express backends, and just to know that it is so commonly used, and there are all of these
helpful methods to make our lives easier and work with this powerful database.
As I showed you earlier, this is the NPM package that we're going to be using, node-postgres, and
it's known as pg on NPM.
We're going to be installing it,
and it will help us vastly simplify all of the operations we need to do with our Postgres database.
And you can see by the number of weekly downloads that it is very, very heavily leaned upon by developers.
So we are doing all of the right things that professional developers do.
In order to get started, the first thing we're going to need to do is to install two pieces of software,
the first is the Postgres server.
This is what's actually going to stand up a Postgres server on our local computer.
Of course, if you're working with a remote server then you wouldn't need this,
but in our case, we do need to run this on our computer, so we don't need to pay for storage costs
and all of that.
Now, the second piece of software we're going to install is something called, pgAdmin,
and this is a UI or a user interface for us to be able to tap into our Postgres server, because otherwise
we'd have to be running terminal commands.
We'd have to connect to our Postgres server using the command line.
And by using pgAdmin, we vastly simplify it, and we're able to view, and organize, and query our data
in a graphical user interface.
So these are the two things we need to install.
And if you head over to the next lesson, you'll see the instructions for how to download and install
these pieces of software on your computer system.
So for all of that and more, I'll see you on the next lesson.