PostgreSQL with Sequelize
Learn to work with PostgreSQL using Sequelize ORM for Node.js applications
60 min•By Priygop Team•Last updated: Feb 2026
Introduction to PostgreSQL
PostgreSQL is a powerful, open-source relational database system. Sequelize is a promise-based Node.js ORM for PostgreSQL, MySQL, MariaDB, SQLite, and Microsoft SQL Server.
Setting Up PostgreSQL with Sequelize
Example
// Install dependencies
npm install sequelize pg pg-hstore
// Database configuration
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'postgres',
logging: false
});
// Test connection
async function testConnection() {
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
}
testConnection();
// Define a Model
const { DataTypes } = require('sequelize');
const User = sequelize.define('User', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
age: {
type: DataTypes.INTEGER,
validate: {
min: 0,
max: 120
}
}
}, {
timestamps: true
});
// Sync the model with database
sequelize.sync({ force: true })
.then(() => {
console.log('Database synced');
})
.catch(err => {
console.error('Error syncing database:', err);
});