Pg-promise: Connection strings do not work with 6.x default settings

Created on 26 Jun 2017  路  5Comments  路  Source: vitaly-t/pg-promise

By default pg-promise 6.x uses a pool. As far as I could understand the code, pg-promise passes the connection details to pg-pool. pg-pool however does not support connection strings.

It looks like in order to support connection strings pg-promise would have to do the parsing. I personally worked around this directly using pg-connection-string before passing the connection details to pg-promise.

bug dependency issue

Most helpful comment

Fixed in v6.1.1.

Now connection strings work in 6.x the same as they did in 5.x:

const db = pgp('postgres://user:password@host:port/database');

All 5 comments

@joux3 you are right. This was the only thing on my mind, to document that connection strings do not quite work in 6.x, I just didn't realize they do not work at all.

I will re-test it and post an update.

According to this: https://node-postgres.com/features/connecting#connection-uri

You can do pgp({conectionString: 'postgresql://...'}) and it will work well with connection strings.

Somehow I've missed that in my tests and documentation. Will be updating it shortly.

P.S. I might just automatically do the conversion of a string into such an object.

Fixed in v6.1.1.

Now connection strings work in 6.x the same as they did in 5.x:

const db = pgp('postgres://user:password@host:port/database');

EDIT: Derp. I found more documentation, and saw that I'm wrong with how to use this. One doeesn't invoke the require value to connect. One invokes that with some options and then uses the return value of that to connect. My bad, and sorry for the noise!

Sorry to comment on an old issue, if I'm missing something or you'd like me to open a different issue, I'm happy to do that.

I'm trying to use a connection string like in the last comment, but I get an error. Here is the attempt to make the connection:

// Verified that env.postgresConnectionString equals ' postgres://user:password@localhost:5432/message_store'
const db = pgPromise(env.postgresConnectionString)

This gives me the error:

TypeError: Invalid initialization options: "postgres://user:password@localhost:5432/message_store"
    at $main (node_modules/pg-promise/lib/main.js:194:19)
    at Object.<anonymous> (src/index.js:18:12)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)

When I try:

const db = pgPromise({ connectionString: env.postgresConnectionString })

I get the following error:

WARNING: Invalid property 'connectionString' in initialization options.

Am I using the connection string correctly, or was this functionality removed? Looking at the code, I see that at https://github.com/vitaly-t/pg-promise/blob/master/lib/main.js#L193 it only wants an object for the connection information and then at https://github.com/vitaly-t/pg-promise/blob/master/lib/main.js#L198 connectionString is not listed as a valid option.

Thanks for the library!

@juanpaco Please follow the getting started steps, you first initialize the library, then connect. In your code you skip the initialization step, hence the error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

normanfeltz picture normanfeltz  路  4Comments

blendsdk picture blendsdk  路  3Comments

ForbesLindesay picture ForbesLindesay  路  3Comments

leemhenson picture leemhenson  路  5Comments

realcarbonneau picture realcarbonneau  路  4Comments