简体   繁体   中英

PG NPM Package Not Connecting to Localhost DB

I'm having trouble getting the pg package working on my local system. 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 .

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.

I've also tried in the node shell in the directory of the project where I installed pg and haven't had any luck.

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 :

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
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM