[英]PG NPM Package Not Connecting to Localhost DB
I'm having trouble getting the pg
package working on my local system. 我在本地系统上无法正常使用
pg
软件包。 I've tried to run the following: 我尝试运行以下命令:
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
});
But I keep getting TypeError: callback is not a function
. 但是我不断收到
TypeError: callback is not a function
。
Is there a setting that I need to change in order to connect to the db via a connection string? 是否需要更改设置才能通过连接字符串连接到数据库? I have tried the username and password that I'm using in
user:password
above on a database on my local machine and I can connect just fine. 我已经在本地计算机上的数据库中尝试了上面的
user:password
使用的user:password
名和密码,并且可以正常连接。
I've also tried in the node
shell in the directory of the project where I installed pg
and haven't had any luck. 我还尝试在安装
pg
的项目目录中的node
外壳中进行测试,但没有任何运气。
Thanks 谢谢
This the error that I get from running the answer below: 这是我从下面的答案中得到的错误:
$ node pg_test.js
error fetching client from pool { [error: password authentication failed for user "jake"]
Directly from documentation at https://github.com/brianc/node-postgres : 直接来自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.