从节点js连接到Postgresql数据库

时间:2020-05-24 18:05:56

标签: node.js postgresql

我想建立一个从我的nodejs脚本到我的数据库连接的连接,但似乎有一个我无法弄清的可疑问题。

此刻,这是我的代码:

const { Pool } = require('pg');
const pool = new Pool({
    user: 'user',
    host: '192.168.1.xxx',
    database: 'database',
    password: 'password',
    port: 5432,
});
pool.on('error', (err, client) => {
    console.error('Error:', err);
});
const query = `SELECT * FROM users`;
pool.connect()
    .then((client) => {
        client.query(query)
            .then(res => {
                for (let row of res.rows) {
                    console.log(row);
                }
            })
            .catch(err => {
                console.error(err);
            });
    })
    .catch(err => {
        console.error(err);
    });

问题似乎出在pool.connect()中,但是我无法理解我所缺少的内容,因为日志中没有错误。我已经使用 npm install --prefix pg 在项目目录中安装了pg模块,并且我知道模块已正确加载。 我编辑了postgresql.conf:

# - Connection Settings -
listen_addresses = '*'

和pg_hba.conf

host    database       user          192.168.1.0/24          md5

使通过lan可以访问数据库,并且看起来很不错,因为我能够成功连接DBeaver之类的应用程序...但是我无法使用NodeJS。

有可能我需要激活某种配置?

0 个答案:

没有答案