Node.js PostgreSQL CRUD example: Rest APIs with Express & Sequelize

BezKoder
3 min readDec 21, 2020

In this tutorial, I will show you step by step to build Node.js PostgreSQL CRUD Restful API example using Express and Sequelize.

Node.js PostgreSQL CRUD Rest API overview

We will build Rest Apis that can create, retrieve, update, delete and find Tutorials by title.

First, we start with an Express web server. Next, we add configuration for PostgreSQL database, create Tutorial model with Sequelize, write the controller. Then we define routes for handling all CRUD operations (including custom finder).

The following table shows overview of the Rest APIs that will be exported:

  • POST /api/tutorials: create new Tutorial
  • GET /api/tutorials: retrieve all Tutorials
  • GET /api/tutorials/[id]: retrieve a Tutorial by :id
  • PUT /api/tutorials/[id]: update a Tutorial by :id
  • DELETE /api/tutorials/[id]: delete a Tutorial by :id
  • DELETE /api/tutorials: delete all Tutorials
  • GET /api/tutorials/published: find all published Tutorials
  • GET/api/tutorials?title=[keyword]: find all Tutorials which title contains keyword

Finally, we’re gonna test the Rest Apis using Postman.

This is our project structure:

Demo Video

This is our Node.js PostgreSQL CRUD example using Express & Sequelize application demo, test Rest Apis with Postman.

Test the APIs

Run our Node.js application with command: node server.js.

Using Postman, we’re gonna test all the Apis above.

  • Create a new Tutorial using POST /tutorials Api
  • Retrieve all Tutorials using GET /tutorials Api
  • Retrieve a single Tutorial by id using GET /tutorials/:id Api
  • Update a Tutorial using PUT /tutorials/:id Api
  • Find all Tutorials which title contains ‘js’: GET /tutorials?title=js
  • Find all published Tutorials using GET /tutorials/published Api
  • Delete a Tutorial using DELETE /tutorials/:id Api
  • Delete all Tutorials using DELETE /tutorials Api

For more details, implementation and Github, please visit:
https://bezkoder.com/node-express-sequelize-postgresql/

For more details, implementation and Github, please visit:
https://bezkoder.com/node-express-sequelize-postgresql/

For Server-side Pagination:
Node.js Express Pagination with PostgreSQL example

You can also find how to build a fullstack system with this app as backend server in the posts:
Vue.js + Node.js Express + PostgreSQL example
Angular 8 + Node.js Express + PostgreSQL example
Angular 10 + Node.js Express + PostgreSQL example
Angular 11 + Node.js Express + PostgreSQL example
React + Node.js Express + PostgreSQL example

Or Security: Node.js JWT Authentication & Authorization with PostgreSQL example

--

--

BezKoder

A passionate engineer in software development, especially web, mobile & cross-platform application. I love sharing knowledge by writing blogs & tutorials.