postgres + sequelize + node,使用凭据连接到主机

时间:2017-03-21 22:59:15

标签: node.js postgresql sequelize.js

我试图使用sequelize访问远程服务器上的postgres数据库而我没有'知道如何。

我尝试了几种方法,但都没有。

我有这个配置文件:

{
  "postgres" : {
    "ip": "xxx.xxx.xxx.xx",
    "port": "5432",
    "user" : "hostUser",
    "password": "hostPassword",
    "db": {
      "name":"dbName",
      "username": "dbUsername",
      "password": "dbPassword"
    }
  }
}

我试过这些方法

方法1:

let sequelize = new Sequelize('postgres://'
  + config.user + ':' 
  + config.password + '@'
  +  config.ip + '/' 
  + config.db.name,
  config.db.username,
  config.db.password,
  {
    dialect : 'postgres'
  }
)

输出: error: password authentication failed for user

方法2:

let sequelize = new Sequelize(
  config.db.name,
  config.db.username,
  config.db.password,
  {
    host: config.ip,
    port: config.port,
    dialect : 'postgres'
  }
)

输出: connect ECONNREFUSED

方法3:

let sequelize = new Sequelize(
  config.db.name,
  config.db.username,
  config.db.password,
  {
    host: config.user + ':' + config.password + '@' + config.ip,
    port: config.port,
    dialect : 'postgres'
  }
)

输出: getaddrinfo ENOTFOUND

版本:

节点:7.7.3

pg:6.1.5

sequelize:3.30.2

postgres:9.5.6

1 个答案:

答案 0 :(得分:0)

解决此问题的步骤如下:

在远程服务器上: -

1- find \ -name "postgresql.conf"找到配置文件的位置

2- sudo nano /path/to/config/postgresql.conf编辑配置文件

3-将此#listen_addresses = 'localhost'更改为此listen_addresses = '*',然后保存并退出

4- find \ -name "pg_hba.conf"查找hba配置文件

5- sudo nano /path/to/config/pg_hba.conf编辑hba配置文件

6-加 host all all 0.0.0.0/0 md5 host all all ::/0 md5

在文件末尾,然后保存并退出

7-运行/etc/init.d/postgresql restart重启postgres

在代码中连接如下: -

let sequelize = new Sequelize(
  config.db.name,
  config.db.username,
  config.db.password,
  {
    host: config.ip,
    port: config.port,
    dialect : 'postgres'
  }
)
相关问题