Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 PG NPM包未连接到本地主机数据库_Javascript_Node.js_Postgresql_Npm - Fatal编程技术网

Javascript PG NPM包未连接到本地主机数据库

Javascript PG NPM包未连接到本地主机数据库,javascript,node.js,postgresql,npm,Javascript,Node.js,Postgresql,Npm,我无法在本地系统上运行pg程序包。我已尝试运行以下操作: var pg = require('pg'); var con_string = "postgres://user:password@localhost:5432/documentation"; var client = new pg.Client(); client.connect(con_string, function(err, res) { // stuff here }); 但是我一直得到类型错误:回调不是一个函数 是否有需

我无法在本地系统上运行
pg
程序包。我已尝试运行以下操作:

var pg = require('pg');
var con_string = "postgres://user:password@localhost:5432/documentation";

var client = new pg.Client();
client.connect(con_string, function(err, res) {
// stuff here
});
但是我一直得到
类型错误:回调不是一个函数

是否有需要更改的设置,以便通过连接字符串连接到db?我在本地机器上的数据库上尝试了上面在
user:password
中使用的用户名和密码,我可以很好地连接

我还尝试了安装
pg
的项目目录中的
节点
shell,但没有任何运气

谢谢

这是我从运行下面的答案中得到的错误:

$ node pg_test.js 
error fetching client from pool { [error: password authentication failed for user "jake"]

直接从文件中获取,网址为:


我应该补充一点,我尝试将其复制并粘贴到项目目录中的脚本中,名为
pg_test.js
,然后运行
node pg_test.js
,打开浏览器,不幸地看到
从池中获取内容时出错…
,因为您的登录名/密码不正确-postgres的默认用户:postgres。这个代码有效。检查连接字符串。尝试通过postgres客户端等进行连接。连接字符串适用于
postgres
用户,但我可以保证我使用的是我的用户密码,我可以使用
psql-U jake-W-d文档,但它不起作用。它不是很清楚地告诉您一个错误吗?错误:用户“jake”的密码身份验证失败。我同意错误是明确的。我只是不明白为什么当auth在命令行上运行时,连接字符串会导致auth失败。因为您从conn string发送“password”作为密码,而在这里不发送任何内容作为密码
psql-U jake-W-d documentation
。这是提示输入密码的标志。我想我可以试着看看它是否在没有提供密码的情况下工作,然后提示我输入密码。我必须在下班后检查。我只是尝试了没有密码的连接字符串,但它没有工作。这让我相信这与设置有关。
var pg = require('pg');
var conString = "postgres://user:password@localhost:5432/documentation";

//this initializes a connection pool
//it will keep idle connections open for a (configurable) 30 seconds
//and set a limit of 10 (also configurable)
pg.connect(conString, function(err, client, done) {
  if(err) {
    return console.error('error fetching client from pool', err);
  }
  client.query('SELECT $1::int AS number', ['1'], function(err, result) {
    //call `done()` to release the client back to the pool
    done();

    if(err) {
      return console.error('error running query', err);
    }
    console.log(result.rows[0].number);
    //output: 1
  });
});