繁体   English   中英

如何使用 Node JS 连接 Redshift 数据库

[英]How to connect Redshift database using Node JS

我无法使用 Node.js 连接到 redshift 数据库。 我正在使用 Node JS 版本14.15.1 这个版本有什么问题吗?

下面的代码,我在本地机器上试过, redshift.js文件

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;

以下是从数据库index.js文件中获取值的代码

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();
        }
      });
    }
  });

如果我运行这段代码没有得到错误,甚至这一行也没有执行console.log('After Connection');

这个库不是很活跃,如上所述,github 从 2020 年开始存在一个未解决的问题。

最好使用pg-promise

在此处附加脚本链接。 https://www.javaniceday.com/post/how-to-connect-to-a-redshift-database-from-node-js

这个包似乎有点被遗弃了,因为这是一个有确切问题的未解决的问题

要解决该问题,您需要手动应用此解决方案

转到node_modules/node-redshift并替换

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

package.json

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

在此目录中运行npm install以更新此包。 之后你的代码将工作

我得到空白连接并且得到连接方法不是 function 错误。

版本升级在 Linux 环境下不起作用。 有没有人找到任何其他解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM