node.js pg.connect 不是一个函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45174120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:22:10  来源:igfitidea点击:

pg.connect not a function?

node.jsherokupg

提问by user1837296

There appears to be a lot of documentation (e.g. https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js, but also elsewhere including this site) indicating that the proper method of connecting with the pg.js Node package is using pg.connect. However, I attempted (after previous problems with my actual code) to test by using the exact code shown on the aforementioned Heroku documentation:

似乎有很多文档(例如https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js,但也包括本站点在内的其他地方)表明与pg.js 节点包正在使用 pg.connect。但是,我尝试(在我的实际代码之前出现问题之后)使用上述 Heroku 文档中显示的确切代码进行测试:

var pg = require('pg');

pg.defaults.ssl = true;
pg.connect(process.env.DATABASE_URL, function(err, client) {
  if (err) throw err;
  console.log('Connected to postgres! Getting schemas...');

  client
    .query('SELECT table_schema,table_name FROM information_schema.tables;')
    .on('row', function(row) {
      console.log(JSON.stringify(row));
    });
});

And I got the error message "pg.connect is not a function". What is going on, and how do I fix it?

我收到错误消息“pg.connect 不是函数”。这是怎么回事,我该如何解决?

回答by robertklep

A new version of pg, namely 7.0.0, was published about 15 hours ago (from the time I'm writing this).

的新版本pg,即 7.0.0,大约在 15 小时前发布(从我写这篇文章开始)。

This version has lots of changes, one of them being that pg.connecthas been hard-deprecated (in other words: removed) in favor of pg.Pool(...).connect(...), as documented here: https://node-postgres.com/guides/upgrading

此版本有很多更改,其中之一pg.connect已被硬弃用(换句话说:已删除)以支持pg.Pool(...).connect(...),如此处所述:https: //node-postgres.com/guides/upgrading

The new method of connecting looks like this:

新的连接方法如下所示:

var pool = new pg.Pool()

// connection using created pool
pool.connect(function(err, client, done) {
  client.query(/* etc, etc */)
  done()
})

// pool shutdown
pool.end()

Lots of older documentation will not reflect these changes, so the example code they use won't work anymore.

许多较旧的文档不会反映这些更改,因此他们使用的示例代码将不再起作用。

You can either try and rewrite the example code so it works in 7.0.0, or explicitly install an older version that will still work with the example code:

您可以尝试重写示例代码,使其在 7.0.0 中工作,或者显式安装仍可与示例代码一起使用的旧版本:

npm install pg@6

回答by rust

pg: postgresql => (https://www.npmjs.com/package/pg)

pg: postgresql => ( https://www.npmjs.com/package/pg)

?? pg.connectis deprecated since version 6.3 ?

?? pg.connect自 6.3 版起已弃用?

BE CAREFUL : Instead there is another method called pool

小心:取而代之的是另一种方法称为 pool

Here is how you can set up node-postgreseasily with express.

以下是node-postgres使用express.

const pg        = require('pg');
const express   = require('express');
const app       = express();

const config = {
    user: 'postgres',
    database: 'YOURDBNAME',
    password: 'YOURPASSWORD',
    port: 5432                  //Default port, change it if needed
};

// pool takes the object above -config- as parameter
const pool = new pg.Pool(config);

app.get('/', (req, res, next) => {
   pool.connect(function (err, client, done) {
       if (err) {
           console.log("Can not connect to the DB" + err);
       }
       client.query('SELECT * FROM GetAllStudent()', function (err, result) {
            done();
            if (err) {
                console.log(err);
                res.status(400).send(err);
            }
            res.status(200).send(result.rows);
       })
   })
});

app.listen(4000, function () {
    console.log('Server is running on port 4000');
});

回答by Keval Bhogayata

If you want to stick with the code you have you can use an older version of Postgres.

如果您想坚持使用现有代码,可以使用较旧版本的 Postgres。

First, apply:

首先,申请:

npm uninstall postgresql

and then install version 6.1.2 (which is compatible with the code you mentioned):

然后安装 6.1.2 版(与您提到的代码兼容):

npm install [email protected]

回答by Krishna Prasad D

var express = require('express');
var app = express();
const pgp = require('pg-promise')();
var connectionString = "";
var parse = require('pg-connection-string').parse;

try {
    var connectionString = "postgres://USERNAME:@localhost:5432/DBNAME";
    var config = parse(connectionString);
    config.password = "PASSWORD";
    var dbcon = pgp(config);
    app.set('dbCon', dbcon);
} 
catch (error) {
    console.log("DB error")
} 

module.exports = app;

module.exports = 应用程序;