'How to connect Redshift database using Node JS
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');
Solution 1:[1]
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
Solution 2:[2]
This library is not very active, as mentioned above there's an open issue in github from 2020.
It's better to use pg-promise
Attaching a script link here. https://www.javaniceday.com/post/how-to-connect-to-a-redshift-database-from-node-js
Solution 3:[3]
Instead of Raw Connection use Connection Pooling https://github.com/dmanjunath/node-redshift#connection-pooling
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pavel Slepiankou |
| Solution 2 | sudonitin |
| Solution 3 | krishna kumar Balasubramaniam |
