Getting Started with PostgreSQL in your Node.js app

*This article is in progress.

Are you thinking about using PostgreSQL (Postgres) in your Node.js app? Here’s how to get started, using popular npm modules Sequelize and Express.

First: make sure you have already installed npm and Postgres. To verify whether you have installed npm, type the following in your terminal and make sure it gives you a version number.

$ npm -v

I can’t remember installing Postgres, as it was a while back and there are a few ways to do it. I do know that I have this adorable app on my mac, though. I seem to get a version number when I enter the following in the command line:

$ psql --version

In your package.json file, you’ll want to make sure that you have a section called “dependencies” and that these include both “express” and “sequelize”. The dependencies should look something like:

"dependencies": {
//you may have many more modules than this, depending on the needs of your app.
"express": "^4.14.0",
"sequelize": "^3.24.2"

If any dependencies are missing, you can run npm install to get them in your app.

npm install[module name] --save

Add a new database. My local setup let me do this pretty easily.

Now that the modules are present, it’s time to tell your Node app where to find the database, and what types of data will be stored there. The first step is to create a new file in your node app’s directory. I called mine “database.js”.

Here’s what goes in the top of the database.js file. This section contains the settings to log in to your database.

'use strict';
const Sequelize = require('sequelize');
// if there is a password, make it the third parameter after the username,
// otherwise, make it null and include a comma after it
const sequelize = new Sequelize('name_of_db', 'database_username', 'database_password', {
host: 'http://localhost:3000',
dialect: 'postgres',
Show your support

Clapping shows how much you appreciated Kate’s story.