node-postgres,连接意外终止

时间:2018-04-06 16:18:42

标签: javascript node.js sequelize.js node-postgres

我正在尝试使用node-postgres连接到远程数据库。

我可以使用psql客户端进行连接,但在尝试运行时遇到错误# sets the source server as the FIRST server in the list $sourceSRV = (Get-Content ./servers.txt -totalcount 1) $servers = Get-Content ./servers.txt #Variables for code directories $code_src01 = Get-ChildItem -Recurse -path "E$\appdirectory" Foreach ($server in $servers) { Compare-Object -ReferenceObject $sourceSRV$code_src01 - DifferenceObject $server$code_src01 } (使用与psql客户端相同的连接字符串):

Connection terminated unexpectedly

我也一直试图与Sequelize ORM联系,但也遇到了同样的错误。

@EDIT

使用本机模式修复了使用pg和sequelize

进行客户端查询的问题

const { Pool, Client } = require('pg') const connectionString = '...' const pool = new Pool({ connectionString: connectionString, }) pool.query('SELECT NOW()', (err, res) => { console.log(err, res) pool.end() }) const client = new Client({ connectionString: connectionString, }) client.connect() client.query('SELECT NOW()', (err, res) => { console.log(err, res) client.end() })

5 个答案:

答案 0 :(得分:1)

使用可能要花费数小时的流程,我找到了使用Pool的解决方案,但是将idleTimeoutMillisconnectionTimeoutMillis都设置为0。示例:

const { Pool } = require('pg')

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

答案 1 :(得分:0)

使用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());

Sequelize:

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

答案 2 :(得分:0)

尝试一下:

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!");
});

答案 3 :(得分:0)

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

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

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

答案 4 :(得分:0)

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

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