[英]PG NPM Package Not Connecting to Localhost DB
我在本地系统上无法正常使用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
});
但是我不断收到TypeError: callback is not a function
。
是否需要更改设置才能通过连接字符串连接到数据库? 我已经在本地计算机上的数据库中尝试了上面的user:password
使用的user:password
名和密码,并且可以正常连接。
我还尝试在安装pg
的项目目录中的node
外壳中进行测试,但没有任何运气。
谢谢
这是我从下面的答案中得到的错误:
$ node pg_test.js
error fetching client from pool { [error: password authentication failed for user "jake"]
直接来自https://github.com/brianc/node-postgres上的文档:
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
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.