Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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
Node.js 无法在Nodejs中连接本地PostgreSQL数据库_Node.js_Database_Postgresql_Express_Node Postgres - Fatal编程技术网

Node.js 无法在Nodejs中连接本地PostgreSQL数据库

Node.js 无法在Nodejs中连接本地PostgreSQL数据库,node.js,database,postgresql,express,node-postgres,Node.js,Database,Postgresql,Express,Node Postgres,我使用pg连接Node.js服务器中的PostgreSQL本地数据库 一切正常,但突然停止工作 这是我的代码: const {Pool} = require('pg') const connectionString = process.env.DATABASE_URL; const ssl = process.env.NODE_ENV == 'production' ? '?ssl=true' : ''; const pool = new Pool({ connectionString:

我使用
pg
连接Node.js服务器中的
PostgreSQL
本地数据库

一切正常,但突然停止工作

这是我的代码:

const {Pool} = require('pg')
const connectionString = process.env.DATABASE_URL;
const ssl = process.env.NODE_ENV == 'production' ? '?ssl=true' : '';
const pool = new Pool({
    connectionString: connectionString + ssl
});

const db = {
    pool: pool,
    test: async () => {
        try {
            console.log('before connection');
            let result = await pool.query('SELECT $1::text as status', ['connected']);
            console.log('connected')
            return result.rows[0].status;
        } catch (error) {
            console.error('Unable to connect to the database:', error);
            return null;
        }
    }
}

module.exports = db;
这里我称之为:

const express = require('express')
const Router = express.Router();
const db = require('./database')

Router.get('/testdb', async (req, res) => {
    try {
        let result = await db.test();
        res.status(200).send(result);
    } catch (error) {
        res.status(500).send(error);
    }
})
它卡在
池中。查询
'在记录“连接”之前,不会引发任何错误


同样的代码也适用于我的远程数据库,我可以用pgAdmin连接本地数据库。

我在节点版本中发现了问题-几天前我将其更新为
14.5.0


将其修改为
12.18.2 LTS
解决问题。

这是
pg
和node.js 14版本之间的不兼容问题。另请参阅以下跟踪和修复此问题的GitHub问题:

  • 拉取请求:
tl;dr:将
pg
升级到大于8.0.3的版本(
pg>=8.0.3