{"id":1971,"date":"2021-05-01T12:00:00","date_gmt":"2021-05-01T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/build-a-rest-api-with-node-js-and-postgresql-get-post-putdelete-step-by-step\/"},"modified":"2026-07-05T03:25:25","modified_gmt":"2026-07-05T01:25:25","slug":"build-a-rest-api-with-node-js-and-postgresql-get-post-putdelete-step-by-step","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/build-a-rest-api-with-node-js-and-postgresql-get-post-putdelete-step-by-step\/","title":{"rendered":"Build a REST API With Node.js and PostgreSQL (GET, POST, PUT,DELETE) \u2013 Step by Step"},"content":{"rendered":"<p>In this lesson, you will learn in few minutes how to build a simple REST API with Node.js and PostgreSQL. This REST API would serve a list of users. So we would setup PostgreSQL and create a database and a users table. Then we would also setup Node.js and create database connection.<\/p>\n<p>Here&#8217;s what we would cover today<\/p>\n<ol>\n<li><a href=\"#t1\">Setup PostgreSQL\/Nodejs<\/a><\/li>\n<li><a href=\"#t2\">Create Database Connection<\/a><\/li>\n<li><a href=\"#t3\">Create the Server and Client<\/a><\/li>\n<li><a href=\"#t4\">Get All Users<\/a><\/li>\n<li><a href=\"#t5\">Get User By Id<\/a><\/li>\n<li><a href=\"#t6\">Add New User<\/a><\/li>\n<li><a href=\"#t7\">Update User<\/a><\/li>\n<li><a href=\"#t8\">Delete User<\/a><\/li>\n<li><a href=\"#t9\">Next Steps<\/a><\/li>\n<\/ol>\n<p>So let&#8217;s get started!<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Setup PostgreSQL\/Nodejs<\/strong><\/h4>\n<p>Interestingly, both PostgreSQL and Node.js are free!<\/p>\n<p><a href=\"https:\/\/www.postgresql.org\/download\/\" target=\"_blank\" rel=\"noopener\">Download PostgreSQL from here<\/a><\/p>\n<p><a href=\"https:\/\/nodejs.org\/en\/download\/\" target=\"_blank\" rel=\"noopener\">Download Node.js from here<\/a><\/p>\n<p>Install both packages. Please watch the video for the step by step. But it&#8217;s quite easy and clear.<\/p>\n<p>Run this command to install PostgreSQL<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">npm install pg --save\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Create Database Connection<\/strong><\/h4>\n<p>Create a file called connection.js. This file would hold the connection data as shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">const<\/span> {Client} <span style=\"color: #333333;\">=<\/span> require(<span style=\"background-color: #fff0f0;\">'pg'<\/span>)\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">const<\/span> client <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Client({\r\n    host<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"localhost\"<\/span>,\r\n    user<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"postgres\"<\/span>,\r\n    port<span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">5432<\/span>,\r\n    password<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"rootUser\"<\/span>,\r\n    database<span style=\"color: #333333;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"postgres\"<\/span>\r\n})\r\n\r\nmodule.exports <span style=\"color: #333333;\">=<\/span> client\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Create the Server and Client<\/strong><\/h4>\n<p>Node.js allows us to create a server. Now you need to create a second file. I call it api.js (but you can give it any name).<\/p>\n<p>Write the following code inside. This code creates a server listening at port 3300. Then a client is create as well that connects to the server.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">const<\/span> client <span style=\"color: #333333;\">=<\/span> require(<span style=\"background-color: #fff0f0;\">'.\/connection.js'<\/span>)\r\n<span style=\"color: #008800; font-weight: bold;\">const<\/span> express <span style=\"color: #333333;\">=<\/span> require(<span style=\"background-color: #fff0f0;\">'express'<\/span>);\r\n<span style=\"color: #008800; font-weight: bold;\">const<\/span> app <span style=\"color: #333333;\">=<\/span> express();\r\n\r\napp.listen(<span style=\"color: #0000dd; font-weight: bold;\">3300<\/span>, ()<span style=\"color: #333333;\">=&gt;<\/span>{\r\n    console.log(<span style=\"background-color: #fff0f0;\">\"Sever is now listening at port 3000\"<\/span>);\r\n})\r\n\r\nclient.connect();\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Add the BodyParser:<\/strong> This is used to handle conversion to and from json.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">const<\/span> bodyParser <span style=\"color: #333333;\">=<\/span> require(<span style=\"background-color: #fff0f0;\">\"body-parser\"<\/span>);\r\napp.use(bodyParser.json());\r\n<\/pre>\n<p>You also need to install body-parser using npm install (see the video)<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Get All Users<\/strong><\/h4>\n<p>For GET requests, we use app.get() function. This function takes two parameters: the route \/users\u00a0 and a callback. The callback is an arrow function that executes when a request is received. The callback take two parameter: request and response. Inside the callback, we use the client to query the database and\u00a0 then send the result back.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">app.get(<span style=\"background-color: #fff0f0;\">'\/users'<\/span>, (req, res)<span style=\"color: #333333;\">=&gt;<\/span>{\r\n    client.query(<span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>Select <span style=\"color: #333333;\">*<\/span> from users<span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>, (err, result)<span style=\"color: #333333;\">=&gt;<\/span>{\r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #333333;\">!<\/span>err){\r\n            res.send(result.rows);\r\n        }\r\n    });\r\n    client.end;\r\n})\r\nclient.connect();\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Get User By Id<\/strong><\/h4>\n<p>The code below is used to get a single user by id. Take note of how the parameter is passed in the url.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">app.get(<span style=\"background-color: #fff0f0;\">'\/users\/:id'<\/span>, (req, res)<span style=\"color: #333333;\">=&gt;<\/span>{\r\n    client.query(<span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>Select <span style=\"color: #333333;\">*<\/span> from users where id<span style=\"color: #333333;\">=<\/span>${req.params.id}<span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>, (err, result)<span style=\"color: #333333;\">=&gt;<\/span>{\r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #333333;\">!<\/span>err){\r\n            res.send(result.rows);\r\n        }\r\n    });\r\n    client.end;\r\n})\r\nclient.connect();\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Add New User<\/strong><\/h4>\n<p>You can post a new user using the code below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">app.post(<span style=\"background-color: #fff0f0;\">'\/users'<\/span>, (req, res)<span style=\"color: #333333;\">=&gt;<\/span> {\r\n    <span style=\"color: #008800; font-weight: bold;\">const<\/span> user <span style=\"color: #333333;\">=<\/span> req.body;\r\n    <span style=\"color: #008800; font-weight: bold;\">let<\/span> insertQuery <span style=\"color: #333333;\">=<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>insert into users(id, firstname, lastname, location) \r\n                       values(${user.id}, <span style=\"background-color: #fff0f0;\">'${user.firstname}'<\/span>, <span style=\"background-color: #fff0f0;\">'${user.lastname}'<\/span>, <span style=\"background-color: #fff0f0;\">'${user.location}'<\/span>)<span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>\r\n\r\n    client.query(insertQuery, (err, result)<span style=\"color: #333333;\">=&gt;<\/span>{\r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #333333;\">!<\/span>err){\r\n            res.send(<span style=\"background-color: #fff0f0;\">'Insertion was successful'<\/span>)\r\n        }\r\n        <span style=\"color: #008800; font-weight: bold;\">else<\/span>{ console.log(err.message) }\r\n    })\r\n    client.end;\r\n})\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t7\">7. Update User Details<\/strong><\/h4>\n<p>Basically, the the update code follows the same pattern:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">app.put(<span style=\"background-color: #fff0f0;\">'\/users\/:id'<\/span>, (req, res)<span style=\"color: #333333;\">=&gt;<\/span> {\r\n    <span style=\"color: #008800; font-weight: bold;\">let<\/span> user <span style=\"color: #333333;\">=<\/span> req.body;\r\n    <span style=\"color: #008800; font-weight: bold;\">let<\/span> updateQuery <span style=\"color: #333333;\">=<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>update users\r\n                       set firstname <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">'${user.firstname}'<\/span>,\r\n                       lastname <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">'${user.lastname}'<\/span>,\r\n                       location <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">'${user.location}'<\/span>\r\n                       where id <span style=\"color: #333333;\">=<\/span> ${user.id}<span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>\r\n\r\n    client.query(updateQuery, (err, result)<span style=\"color: #333333;\">=&gt;<\/span>{\r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #333333;\">!<\/span>err){\r\n            res.send(<span style=\"background-color: #fff0f0;\">'Update was successful'<\/span>)\r\n        }\r\n        <span style=\"color: #008800; font-weight: bold;\">else<\/span>{ console.log(err.message) }\r\n    })\r\n    client.end;\r\n})\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t8\">8. Delete a User<\/strong><\/h4>\n<p>The delete code is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">app.<span style=\"color: #008800; font-weight: bold;\">delete<\/span>(<span style=\"background-color: #fff0f0;\">'\/users\/:id'<\/span>, (req, res)<span style=\"color: #333333;\">=&gt;<\/span> {\r\n    <span style=\"color: #008800; font-weight: bold;\">let<\/span> insertQuery <span style=\"color: #333333;\">=<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span><span style=\"color: #008800; font-weight: bold;\">delete<\/span> from users where id<span style=\"color: #333333;\">=<\/span>${req.params.id}<span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>\r\n\r\n    client.query(insertQuery, (err, result)<span style=\"color: #333333;\">=&gt;<\/span>{\r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #333333;\">!<\/span>err){\r\n            res.send(<span style=\"background-color: #fff0f0;\">'Deletion was successful'<\/span>)\r\n        }\r\n        <span style=\"color: #008800; font-weight: bold;\">else<\/span>{ console.log(err.message) }\r\n    })\r\n    client.end;\r\n})\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t9\">9. Next Steps<\/strong><\/h4>\n<p>Thumbs up to you if you&#8217;ve come this far!<\/p>\n<p>Now I recommend we do the same using MySQL. The procedure is similar with just minor difference relating to the connection. I&#8217;ll leave this up to you as a home.<\/p>\n<p>So we succeeded in building the API and creating database connection. Once you have an API (Application Programming Interface), then you also need a UI(User Interface). In the next lesson, we would build the UI using Angular 11, Angular Materials and Bootstrap.<\/p>\n<p>It&#8217;s better you subscribe to my YouTube Channel so that when the next lesson come out in few days, you&#8217;ll get notified. Also find the complete code for this lesson in my GitHub Repository.<\/p>\n<p>Thanks and happy learning! I remain Kindson The Genius!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, you will learn in few minutes how to build a simple REST API with Node.js and PostgreSQL. This REST API would serve &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[289],"tags":[],"class_list":["post-1971","post","type-post","status-publish","format-standard","hentry","category-rest-web-services"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1971"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1971\/revisions"}],"predecessor-version":[{"id":2139,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1971\/revisions\/2139"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}