How to connect Redshift database using Node JS
Asked Answered
E

1

7

I am not able to connect to the redshift database using Node. I am using Node JS version 14.15.1. Is there any issue related to this version?

The following code, I have tried in my local machine, redshift.js file

var Redshift = require('node-redshift');
 
var client = {
    user: 'user',
    database: 'db',
    password: 'password',
    port: port,
    host: 'hostname',
};

var redshiftClient = new Redshift(client, {rawConnection:true});
 
module.exports = redshiftClient;

The following is the code to get the values from database, index.js file

var redshiftClient = require('./redshift.js');

console.log('Before Connection');
redshiftClient.connect(function(err){
console.log('After Connection');
    if(err) throw err;
    else{
      redshiftClient.query('SELECT * FROM "customer"', {raw: true}, function(err, data){
        if(err) throw err;
        else{
          console.log(data);
          redshiftClient.close();
        }
      });
    }
  });

If I run this code not getting the error and even this line is also not executed console.log('After Connection');

Empedocles answered 17/6, 2021 at 9:6 Comment(4)
I have used the node version 12.13.1 and issue is solved for me.Empedocles
The same code I have used in AWS Lambda function but getting empty response. Why?Empedocles
Please follow the link, this code works for me. #68080767Empedocles
Instead of Raw Connection use Connection Pooling <github.com/dmanjunath/node-redshift#connection-pooling>Shay
F
3

The package seems a bit abandoned as here's an open issue with exact issue

To solve that you need to apply this solution manually

go to node_modules/node-redshift and replace

  "dependencies": {
    "commander": "^2.9.0",
    "migrate": "^0.2.2",
    "pg": "^6.1.2",
    "sql-bricks": "^1.2.3"
  },

in package.json to

  "dependencies": {
    "commander": "^2.9.0",
    "migrate": "^0.2.2",
    "pg": "^8.1.5",
    "sql-bricks": "^1.2.3"
  },

run npm install in this directory in order to update this package. After that your code will work

Freeze answered 17/6, 2021 at 12:10 Comment(1)
In node_modules/node-redshift folder you have mentioned "pg": "^6.1.2", but in the package.json file the version for the pg is different ``` "pg": "^8.1.5",```. Is it the same?Empedocles

© 2022 - 2024 — McMap. All rights reserved.