question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How I can have two different connection pools for two different databases?

See original GitHub issue

It is possible to set pool size in pg.defaults. However let’s say I have two databases and two separate connection strings for them:

postgres://localhost/db1
postgres://localhost/db2

And I want to have two different pool sizes for each of them. For example max 20 connections for db1 and max 100 connections for db2. How I can do that? Changing pool size in pg.defaults will set it globally…

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
rpedelacommented, Apr 1, 2016

I don’t think it is supported. I recommend using pgbouncer for connection pooling. It is more flexible, faster, mature than the node-postgres connection pooling. There are other options too from the Postgres community. If you use pgbouncer, then you can open/close single connections in your JS code without any significant performance penalty.

3reactions
rpedelacommented, Apr 1, 2016

You configure pgbouncer, have it run as a daemon, and then you make Postgres queries to pgbouncer (default port 6543) in Node.js like normal without using the pooling code like the example in the README:

var pg = require('pg');

var conString = "postgres://username:password@localhost:6453/database";

var client = new pg.Client(conString);
client.connect(function(err) {
  if(err) {
    return console.error('could not connect to postgres', err);
  }
  client.query('SELECT NOW() AS "theTime"', function(err, result) {
    if(err) {
      return console.error('error running query', err);
    }
    console.log(result.rows[0].theTime);
    //output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
    client.end();
  });
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I use one pooled datasource for multiple databases ...
If you have 2 databases, DB1 and DB2, and want a connection pool, you need to have prepared connections for both databases. If...
Read more >
How to create one Connection Pool for Two Different ...
You create 2 databases in the physical layer, each one with its own connection pool and then you can do your cross joins...
Read more >
Connection pooling and multiple databases
We use multiple databases at work and we have a “reporting” connection pool using a particular user account that has select-only access to...
Read more >
Multiple connection pools : brief refresh of common rules
You can have multiple connection pools inside the same database object and they can use different database users with different access and ...
Read more >
How to create a Multi-Database Pool in HikariCP
This post is about a program you can use if you are using multiple databases, and data sources for connection pooling, using core...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found