Pg-promise: Connecting with "postgres://" does not work as it expects an object

Created on 20 Aug 2015  Â·  16Comments  Â·  Source: vitaly-t/pg-promise

The README says that you can use either an Object or an String to connect, but following code is not working:

var db = pgp("postgresq://user:password@host:port/database");

getting a run-time exception:

        throw new Error("Invalid parameter 'options' specified.");

Switched to an Object representation and worked like a charm.

no-issue question

Most helpful comment

I should RTFM more carefully.
Finally figured it out, thanks for answering.

All 16 comments

You make the wrong call into the library object, not following the Getting Started instructions.

Your pgp object is set to require("pg-promise"), as opposed to:

require("pg-promise")(/*options*/)

As a result, you are making a call to initialize the library with options, but instead you are passing it the connection string, which is part of the next step - database initialization.

And as the library expects optional parameter options, giving it a connection string instead results in the error that you are seeing.

I should RTFM more carefully.
Finally figured it out, thanks for answering.

It's weird that you have to call pgp() twice. I just had the same issue. There should probably be a line in the manual clarifying it a little so others don't struggle too.

The library clearly defines it as a two-step process:

  1. Load and initialize the library: var pgp = require('pg-promise')(/*options*/)
  2. Create the database instance: var db = pgp(connection);

And it is everywhere, in all the examples.

Such pattern is used in many libraries, I don't know why it seems weird. I will consider amending the manual though.

You are the second person asking about it, since ever, so I don't know if there is really an issue there....

@vitaly-t because 'options' is in comments I didn't "see" the extra function call. I don't know if it's the same reason for the others.

@tjmcewan Thank you! I wasn't sure how to update the docs to make it more clear. Plus all the examples show the same thing.

I can't say this is ideal, no, but this is the way it has been implemented from day one, so for the sake of compatibility... The bottom line, once you figure that once, you won't make the same mistake again ;)

I will consider an update, should I start on version 3.0.0

Cheers!

I'm not commenting on the api, just the way I read the docs. :)
Maybe pass in an actual options object with some defaults instead of the
commented text? Just to make it clearer that it's not something that can be
ignored.

Thanks!
On Wed, 20 Jan 2016 at 15:56, Vitaly Tomilov [email protected]
wrote:

@tjmcewan https://github.com/tjmcewan Thank you! I wasn't sure how to
update the docs to make it more clear. Plus all the examples show the same
thing.

I can't say this is ideal, no, but this is the way it has been implemented
from day one, so for the sake of compatibility... The bottom line, once you
figure that one out once, you won't make the same mistake again ;)

I will consider an update, should I start on version 3.0.0

Cheers!

—
Reply to this email directly or view it on GitHub
https://github.com/vitaly-t/pg-promise/issues/39#issuecomment-173087827.

@tjmcewan null / undefined or {} are the defaults.

I didn't want newcomers to focus on something that is optional, that's why I did it that way...

Version 3.0 is out, and this small amendment is there also.

I also ran into this problem, this issue thread helped solve it for me. What made it hard for me to notice was that I am using ES6 import statements instead of require. The docs go from the require description to connection settings.

Getting Started explicitly says:

// Loading and initializing the library:
var pgp = require('pg-promise')({
    // Initialization Options
});

Two-step is what it takes to set up universal use of promises throughout the library, unfortunately.

I'm gonna look at making the error more intelligent/descriptive.

This functionality has been improved in version 3.4.3.

IMO these docs could be updated, just arrived here searching for answers with the same confusion:
https://github.com/vitaly-t/pg-promise/wiki/Connection-Syntax

The same confusion? The docs have been rewritten since. There should be no confusion anymore, if you just follow the documentation.

If one lands directly on the Connection Syntax wiki page I linked there's no indication of the correct way to require the pgp module - i.e. const pgp = require('pg-promise')({}).

That wiki page just needs the update you mentioned earlier in this thread:

// Loading and initializing the library:
var pgp = require('pg-promise')({
    // Initialization Options
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

realcarbonneau picture realcarbonneau  Â·  4Comments

cmelone picture cmelone  Â·  3Comments

jabooth picture jabooth  Â·  4Comments

Juanflugel picture Juanflugel  Â·  3Comments

vitaly-t picture vitaly-t  Â·  3Comments