brianc / node-postgres

PostgreSQL client for node.js.
https://node-postgres.com
MIT License
12.17k stars 1.21k forks source link

"Server does not support SSL connections" when connectionString is undefined #1391

Open Yitzi2 opened 7 years ago

Yitzi2 commented 7 years ago

When pg.Pool is accidentally passed "undefined" as its connectionString, it (at least on my Windows machine) results in the error message "Server does not support SSL connections", which makes debugging more difficult than a more specific error message would.

vitaly-t commented 7 years ago

Simply passing in undefined into the Pool type doesn't and cannot result in anything, because the type doesn't initiate any connection at that point (during the construction).

Please show the complete code example where you are getting the error.

Yitzi2 commented 7 years ago

Sorry. The following code is sufficient to produce this error message:

const pg = require('pg'); pg.defaults.ssl = true; const pool = new pg.Pool({connectionString: undefined}); pool.query("foo");

If I leave out the line "pg.defaults.ssl = true;", it gives the error message "password authentication failed", which is also misleading. Ideally it would note that the connection string is undefined.

vitaly-t commented 7 years ago

The error is absolutely correct.

Since connectionString is undefined, it is ignored. And then the driver tries to connect using all of the default parameters, of which you set only ssl = true. So while trying to connect that way, the first error that occurs is SSL-related. And if you do not activate the SSL, then the error is with the authentication.

Yitzi2 commented 7 years ago

While it is correct in the sense that nothing is failing to work according to code, it is still a misleading error message; this is more a "feature add request" issue than a "bug" issue.

vitaly-t commented 7 years ago

How is it misleading, if the error is correct?

Yitzi2 commented 7 years ago

Because if the connection string was unintentionally undefined (a fairly common error type), it makes it look like it's attempting to connect to the intended server and failing on that end. Even changing the error message to say which server is being connected to would give some clues as to where the error lies.

brianc commented 7 years ago

@Yitzi2 can you provide a small code example demonstrating the problem?

vitaly-t commented 7 years ago

He provided the example above. But in mind the problem is fictional.

brianc commented 7 years ago

Oh right - missed it! I was scanning for code-formatted text. To restate what @vitaly-t said & agree with his point: The connection parameters are resolved w/ a combo of the connection string as well as environment variables and pg.defaults - passing undefined as a connection string is perfectly reasonable as other things will be looked at for connection params.

I could see adding additional info to error objects such as the Client instance on which the error occurred so you could inspect the instance & get the connection parameters off of it, but it's not a high priority thing for me right now. I'll keep this open as a reminder incase I get to it in the future.

Yitzi2 commented 7 years ago

Ok, thanks. Additional info to the error message (even if it's just what was connected to) would definitely help with debugging.

tdmalone commented 6 years ago

I experienced the same issue and got quite confused - tried turning off SSL support, still got the error, scratched my head for awhile... and then Googling lead me here. Of course, I had forgotten to actually pass in the connection parameter!

While it's clearly a user error, I do agree that a better error message would help the debugging process :)

farhankassam2 commented 3 years ago

Agreed. Googling led me here and now I know that my connectionString was left undefined. It is so weird because I do define a connectionString in my environment variables within a .env file locally and I access it using react-native-dotenv. However, the accessed value remained undefined for some reason. It may be a problem with the npm package I am using (above).

I wanted to post a screenshot of an example in my code, which shows that the connectionString was left undefined when registering a FastifyInstance with theconnectionString. I hope this satisfies @brianc 's request on August 2nd, 2017 (lol). So here it is:

Screen Shot 2020-09-12 at 9 37 33 PM

The error was occurring at the call to the database to access the items from an SQL query as follows:

Screen Shot 2020-09-12 at 9 49 15 PM

After manually specifying the DB_STRING and not using the react-native-dotenv package, I successfully got a response after the line that was throwing me an error (the responses is containing the blue highlighted line in the screenshot above).

So indeed, the solution is to not pass in an undefined connectionString when registering any instance of a database connection (in my case I was using Fastify).

proton1k commented 3 years ago

To everyone who uses Firewall or VPN - try turn it off and reconnect. If worked, then your code is fine.

techsin commented 2 years ago

i use connection string for production and env variables for local db, so connectionString is undefined, but pg fails because it still counts connectionString and ignores other variables.

roorkrn commented 10 months ago

I believe there is some issue with PGDRIVER node module version error with below lines. I comment it out in my code and its working fine.

// ssl: { // rejectUnauthorized: false, // },