We are going to set up the connection from our Node.js project to the PostgreSQL database using pg-promise, the package that we have installed recently. Under the app root directory, create the following Node.js file named queries.js:
- The first part of the file contains require, a command to include the bluebird library, then attaches our PostgreSQL RDS connection by Node.js promise:
[root@ip-172-31-95-213 node-api]# vi queries.js
------------------------------------------
var promise = require('bluebird');
var options = {
// Initialization Options
promiseLib: promise
};
var pgp = require('pg-promise')(options);
var connectionString = 'postgres://dba:[email protected]';
var db = pgp(connectionString);
The preceding code uses pg-promise – a Node.js library. This library provides automatic PostgreSQL connections, PostgreSQL transactions, and the PostgreSQL query engine. The pg-promise...