簡體   English   中英

PG NPM軟件包未連接到Localhost數據庫

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM