brianc / node-pg-pool

A connection pool for node-postgres
MIT License
180 stars 64 forks source link

connection string for pg-native #41

Closed nonword closed 6 years ago

nonword commented 7 years ago

I'm trying to use this with pg-native and finding that clients aren't connecting to the connection properties I'm passing in. They instead fall back on environmental variables.

I can see that pg-pool passes the connection hash to the Client constructor, but passes nothing but a cb to the connect method. Pg-native expects connection config in the latter. Furthermore, it expects it as a string, following the libpq convention.

Should I pass in a custom create handler so that I control how (pg-native) clients are created? Or is there a better solution?

My test script:

const Pool = require('pg-pool')

const config = require('config')

// pg.url has form: "postgresql://user@localhost:5432/dbname",
const params = url.parse(config.get('pg.url'))
const auth = params.auth.split(':')

const connectionConfig = { 
  user: auth[0],
  password: auth[1],
  host: params.hostname,
  port: params.port,
  database: params.pathname.split('/')[1],
  ssl: true,
  Client: require('pg-native'),
  min: 4,
  max: 40, 
  idleTimeoutMillis: 1000
}
console.log('Connecting with: ', connectionConfig)
const pool = new Pool(connectionConfig)

pool.connect().then((client) => {
  client.query('select $1::text as name', ['pg-pool']).then((res) => {
    client.release()
    console.log('hello from', res.rows[0].name)
  })  
  .catch((e) => {
    client.release()
    console.error('query error', e.message, e.stack)
  })  
}).catch((e) => {
  console.error('error connecting: ', e.message, e.stack)
})

Note: Commenting out Client: require('pg-native'), causes it to succeed.

jfsimon commented 7 years ago

I succeeded using pooling and pg-native this way:

import pg from 'pg';

const pool = new pg.Pool({
    user: 'user',
    password: 'pass',
    host: 'localhost',
    port: 5432,
    database: 'database',
    Client: pg.native.Client
});
jgeurts commented 6 years ago

Could you connect with the following?

const Pool = require('pg-pool');
const config = require('config')

const pool = new Pool({
  connectionString: config.get('pg.url')
});
nonword commented 6 years ago

I can't reproduce on latest versions of pg-native/pg-pool - nor using versions contemporaneous with the original issue - so I'm going to close this. Sorry for the noise. Thanks!