Node.js wrapper for pgbouncer. All methods are asynchronous, using Q promises.
PgBouncer = require 'pgbouncer'
# create a new PgBouncer instance
# pointing to the location of the pgbouncer.ini file
pgb = new PgBouncer(configFile: '/etc/pgbouncer.ini')
pgb.read()
.then (config) ->
# [pgbouncer] section
# config.pgbouncer.listen_port: 5434
# config.pgbouncer.listen_addr: '127.0.0.1'
# config.pgbouncer.auth_type: 'any'
# [databases] section
# config.databases.mydb1: 'postgresql://localhost/db1'
# config.databases.mydb2: 'postgresql://localhost/db2'
config =
pgbouncer:
listen_port: 5434
listen_addr: '127.0.0.1'
auth_type: 'any'
databases:
mydb1: 'postgresql://localhost/db1'
mydb2: 'postgresql://localhost/db2'
pgb.write(config)
.then -> console.log('done')
If you don't need to change the [pgbouncer]
section, you can also update the [databases]
list only.
databases:
mydb1: 'postgresql://localhost/db1'
mydb2: 'postgresql://localhost/db2'
pgb.writeDatabases(databases)
.then -> console.log('done')
After updating the INI file, you need to manually trigger a reload
command to update the live routing.
pbg.reload()
.then -> console.log('done')
You can get the current routing status from the pgbouncer show databases
command:
pbg.status()
.then (databases) ->
# databases[0].name
# databases[0].host
# databases[0].port
# databases[0].database
# databases[0].force_user
# databases[0].pool_size
You can also execute any valid pgbouncer command:
pgb.execute('show users')
.then (results) -> console.log(results.rows)
Please check pgbouncer documentation for a list of commands.