my name is kinds on the tech pro and in
the previous tutorial i showed you how
to
connect nodes.js to mysql database and
fetch data and as you can see right here
but today i'm going to show you how to
connect node.ds to postgrew sql so here
is my postgresql
i have the default database here and i
have a table inside
i'm going to just open the schemas and
go to these tables called
users and i'm going to save you edit
data and select all rows you can see
there are about three items
or four items in this place so how do we
connect from node.js here
to postgresql and actually fetch and
display the data on the screen on the
console
so the first thing i'd like to do let me
create another file i'm going to call it
database
dot database pg because we already have
database
dot yes all right so it's also very
straightforward so the first thing you
want to do is to create a client
sorry there is you need to initialize by
saying npm
in needs minus y
yeah so you need to initialize yeah
perfect i think it
yeah i think it should be fine and the
next thing you want to do is to install
the pg
package by saying npm and pm
install
install pg and that's the next thing you
want to do
i think i already did it but it's okay
all right so once you take these two
steps you know how to
write the create the client so you have
to create uh
first import the client or require the
client by saying client as a quality
require
i require pg because that's the package
you are going to be using to
do the connection so let's see const
okay so this is a little typo
and the next thing you want to do is to
specify the connection parameters so i'm
going to say cons again please subscribe
if you are just joining me for the first
time
uh subscribe and also let me know if you
have challenges so i'm going to create a
client that's going to be used for this
connection so i'm going to say
client is equal to new
client so in this client you want to
specify the connection parameters for
instance the host
is going to be local hosts local
host and we also need to specify the
user the default user is
postgres postgres if i get this
correctly
and we also need the default port i
didn't change the port so the port
should be five
four three two i think that's the
default and the password
the password i use here should be
um root user
all right and finally you want to
specify
the database so you're going to specify
the database
the name of the database is the default
one as well posts
grace all right okay so now you are
going to say
client dot connect so i'm going to say
client
dot connect so this you are going to
explicitly connect to the client and the
next thing you want to do is to write
your query so i'm going to say
client.query and then you're going to
write the query so i'm going to use
backticks
i'm going to say select start from users
select star from users
and this query takes two parameters
we've written the first one is the
string and the second one is a callback
uh that's going to execute when the
query
returns so i'm going to say it's going
to take two parameters
the results and also arrow if error or
cause
so it's going to take error if error of
course and also the results
and it's going to be an arrow function
and we are now going to write what is
going to happen so
let's start by saying if error occurs we
will just want to print out
if error does not occur so either way so
you can actually play around with this
if error does not occur just print out
the output
res dot rows okay
um but if error occurs so else
else we are going to display the error
message
to the output so i'm going to say
console.log
the arrow message all right
um and finally at the end you are going
to say clients
dot ends perfect so at this point we've
connected to posgrade so let's see if it
all works
so i'm going to first say save
everything and the name of the
the name of the file we are going to
execute is our database.pg
i like to clear my screen so i'm going
to right click and say clay that's fine
so i'm going to say uh node
database pj.js
all right so you can see that we have
our data returned
exactly the same data we have here kind
of saffron with miller and macmills you
can see that
the data comes here so i'm gonna stop
here so i'm going to continue making the
series and i'm going to talk about how
to insert data and how to actually
delete
data and update data but for now i want
to take it bit by bit so i'm going to
stop here please subscribe and also if
you have challenges let me know i remain
kind of the tech pro
and i'm always there for you