Skip to content
Awsaf Alam
GitHubLinkedIn

Getting Started with Sequelize

— Updated — Backend, Postgres, Sequelize — 2 min read

Sequelize is an ORM for Node.js. It works with Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server, and every query returns a promise. It also handles relations between tables, eager and lazy loading, and read replicas.

An ORM library is a completely ordinary library written in your language of choice that encapsulates the code needed to manipulate the data, so you don't use SQL anymore; you interact directly with an object in the same language you're using ~ StackOverflow

So instead of writing SQL strings, you work with JavaScript objects and Sequelize writes the SQL for you. "Promise-based" means each call returns a promise, so you can use await on it.

This guide covers the first step: a Node.js project that connects to a Postgres database. Sequelize works with the other databases too, but the driver package and connection URL change.

Initializing the project

In an empty directory, run:

bash
yarn init -y
yarn add sequelize pg pg-hstore

sequelize is the ORM. pg and pg-hstore are the Postgres driver packages Sequelize needs. They go in normal dependencies, not -D, because your app needs them when it runs, not just while you develop.


Database Initialization

Before writing any code you need a Postgres database to connect to. A local install works fine. If you'd rather use a cloud provider, AWS RDS has a free tier for new accounts. I won't go into the setup here. I wrote up the basics in my notes on setting up a database on AWS RDS. They use MySQL, but creating the RDS instance works much the same way for Postgres.


Database Connection

Create an index.js file. Sequelize can connect with a single connection URL:

js
const { Sequelize } = require('sequelize')
 
const db = new Sequelize('postgres://user:password@localhost:5432/your-db', {
  logging: false,
})

Replace user, password and your-db with your own values. logging: false stops Sequelize from printing every SQL query to the console.

To check the connection, call authenticate(). In a CommonJS file like this one you can't use await at the top level, so wrap it in an async function:

js
async function main() {
  try {
    await db.authenticate()
    console.log('Connection has been established successfully.')
  } catch (error) {
    console.error('Unable to connect to the database:', error)
  } finally {
    await db.close()
  }
}
 
main()

db.close() closes the connection pool so the script can exit. Run it with node index.js. If the URL is right and the database is reachable, you'll see the success message.

From here, the next step is defining models with db.define() and syncing them to tables. The Sequelize model basics guide covers that.


Some Suggested resources

Written by Md Awsaf Alam. I'm an AI Product Engineer and R&D Lead at Sourcy, and an FSE 2026 first author.

AboutLinkedInGoogle Scholar