Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点postgres,连接意外终止_Javascript_Node.js_Sequelize.js_Node Postgres - Fatal编程技术网

Javascript 节点postgres,连接意外终止

Javascript 节点postgres,连接意外终止,javascript,node.js,sequelize.js,node-postgres,Javascript,Node.js,Sequelize.js,Node Postgres,我正在尝试使用节点postgres连接到远程数据库 我可以使用psql客户端进行连接,但在尝试运行此操作时(使用与psql客户端相同的连接字符串),出现了连接意外终止的错误: 我也一直在尝试连接Sequelize ORM,但也出现了同样的错误 @编辑 使用本机模式修复了使用pg和sequelize进行客户端查询的问题 const{Pool,Client}=require('pg')。本机使用pg: import pg from 'pg'; const conStringPri = `pos

我正在尝试使用节点postgres连接到远程数据库

我可以使用psql客户端进行连接,但在尝试运行此操作时(使用与psql客户端相同的连接字符串),出现了连接意外终止的错误

我也一直在尝试连接Sequelize ORM,但也出现了同样的错误

@编辑

使用本机模式修复了使用pg和sequelize进行客户端查询的问题

const{Pool,Client}=require('pg')。本机使用pg:

import pg from 'pg';  

const conStringPri = `postgres://${username}:${password}@${host}/postgres`;
  const Client = pg.Client;
  const client = new Client({connectionString: conStringPri});
  client.connect();

  client.query(`CREATE DATABASE ${dataBaseName}`)
    .then(() => client.end());
续集:

const sequelize = new Sequelize(dbName, username, password, {
  host: host || 'localhost',
  dialect: type || 'postgres',
  operatorsAliases,
  pool: {
    max: 5,
    min: 0,
    idle: 300000,
    acquire: 300000
  },
  port: port || 5432,
  logging: log => console.log('logging:', log)
});

const models = {};
// read all models from same folder
glob.sync(path.join(__dirname, '**/*.js'))
  .forEach(file => {
    const model = sequelize.import(file);
    models[model.name] = model;
  });

Object.keys(models).forEach(model => {
  if (models[model].associate) {
    models[model].associate(models);
  }
});

models.user.create(userObject);
models.user.findAll({where: {name: 'john'}});
试试这个:

var pg = require('pg');
const client = new pg.Client(
{
    user: 'username',
    host: 'host',
    database: 'myDb',
    password: 'secretPswd',
    port: portnum,
});
client.connect(function (err){
    if(err)
        console.log(err);
    else
        console.log("Connected!");
});

我开始遇到同样的问题,但只是在长时间的查询中,我通过在构造函数中设置idleTimeoutMillis找到了一个可能的解决方案,例如设置为20000(默认值为10000)


请参见

处理可能需要数小时的进程,我发现解决方案使用的是
,但设置
idleTimeoutMillis
connectionTimeoutMillis
都是0。例如:

const { Pool } = require('pg')

const pool = new Pool({
  user: 'postgres',
  host: 'localhost',
  database: 'my_database',
  password: 'XXXX',
  port: 5423,
  idleTimeoutMillis: 0,
  connectionTimeoutMillis: 0,
});

以上所有的解决方案对我都不起作用。我在网上搜索过,但没有找到合适的解决办法。最后我发现我在pg客户端输入了错误的端口号

这是我找到实际端口号的地方: 服务>数据库>属性


这是在我的代码中输入的。问题解决了

我第一次在Postgres上遇到这个错误。我不知道Postgres的默认端口是5432。在我的DB节点配置中将端口更改为5432解决了该问题

const db = knex({
    client: 'postgres',
        connection: {
            host: 'localhost',
            user: 'postgres',
            password: 'admin4321',
            database: 'postgres',
            port: 5432,
        }
    })
const db = knex({
    client: 'postgres',
        connection: {
            host: 'localhost',
            user: 'postgres',
            password: 'admin4321',
            database: 'postgres',
            port: 5432,
        }
    })