Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:28:43+00:00 2026-06-09T21:28:43+00:00

node-postgres states the following: node-postgres supports both an ‘event emitter’ style API and a

  • 0

node-postgres states the following:

node-postgres supports both an ‘event emitter’ style API and a ‘callback’ style. The
callback style is more concise and generally preferred, but the evented API can come in
handy. They can be mixed and matched.

With the event emitter API, I can do the following:

var db = new pg.Client("insert-postgres-connection-info");
db.connect();

And then I can use db to execute queries throughout my web app using db.query('sql statement here'). With the callback style, I would do the following each time I want to run a query:

pg.connect(conString, function(err, client) {
  client.query("sql statement", function(err, result) {
    // do stuff
  });
});

So my question is why is it “generally preferred” to use the callback style? Isn’t it inefficient to open a connection each time you do something with the database? What benefits are there from using the callback style?

EDIT

I might be mistaken as to what he means by “callback style” (I’m not kidding, my JavaScript isn’t very strong) but my question is about the method of connection. I assumed the following was the callback style connection method:

// Simple, using built-in client pool

var pg = require('pg'); 
//or native libpq bindings
//var pg = require('pg').native

var conString = "tcp://postgres:1234@localhost/postgres";

//error handling omitted
pg.connect(conString, function(err, client) {
  client.query("SELECT NOW() as when", function(err, result) {
    console.log("Row count: %d",result.rows.length);  // 1
    console.log("Current year: %d", result.rows[0].when.getYear());
  });
});

and the following is the EventEmitter API connection method:

// Evented api
var pg = require('pg'); //native libpq bindings = `var pg = require('pg').native`
var conString = "tcp://postgres:1234@localhost/postgres";

var client = new pg.Client(conString);
client.connect();

If I’m just getting terms mixed up here, my question still remains. pg.connect(do queries) opens a new connection every time you use it (doesn’t it?) whereas

var client = new pg.Client(conString);
client.connect();

opens a connection and then allows you to use client to run queries when necessary, no?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:28:45+00:00Added an answer on June 9, 2026 at 9:28 pm

    The EventEmitter style is more for this type of thing:

    var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John']);
    query.on('row', function(row) {
      console.log(row);
      console.log("Beatle name: %s", row.name); //Beatle name: John
      console.log("Beatle birth year: %d", row.birthday.getYear()); //dates are returned as javascript dates
      console.log("Beatle height: %d' %d\"", Math.floor(row.height/12), row.height%12); //integers are returned as javascript ints
    });
    

    By mixing and matching, you should be able to do the following:

    // Connect using EE style
    var client = new pg.Client(conString);
    client.connect();
    
    // Query using callback style
    client.query("SELECT NOW() as when", function(err, result) {
      console.log("Row count: %d",result.rows.length);  // 1
      console.log("Current year: %d", result.rows[0].when.getYear());
    });
    

    Note that even when using the callback style, you wouldn’t open a connect every time you want to execute a query; most likely, you’d open a connection when the application starts and use it throughout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Node.js server is works on event based models where callback functions are supported. But
In Node 0.4.2, is it possible for http.request() to return both through the callback
Since Node is asynchronous, I'm having problems trying to get a callback to return
I'm trying to do an insert or update in a postgres database using node.js
I'm getting a javascript error returned from postgres using node 'pg'. Here is the
node is crashing at the following line: var tcp = require('tcp'), error text: node.js:201
I'm using the node-postgres module for node.js and I have a problem inserting data.
I'm migrating toward node.js 0.6.12 and now got the following error messages when using
installing node-v0.4.8 on mac os x leopard, I get the following error: ../src/node_crypto.cc:2917: error:
Is there a cross database ORM mapper for Node.js which provides a consistent API

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.