Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pg client fails to connect (silently) using Node 14 #2317

Closed
martinbliss opened this issue Aug 23, 2020 · 31 comments
Closed

pg client fails to connect (silently) using Node 14 #2317

martinbliss opened this issue Aug 23, 2020 · 31 comments

Comments

@martinbliss
Copy link

The pg client seems to fail quietly when connecting to postgress when using Node 14.x. Tested using Docker.

Dockerfile

FROM node:14.0.0-alpine3.10 as builder
RUN apk add postgresql-client
WORKDIR /src
COPY . .
ENTRYPOINT [ "/bin/sh" ]

package.json

{
  "devDependencies": {
    "pg": "^8.3.2",
    "ts-node": "^8.10.2",
    "typescript": "^3.7.5"
  }
}

test-pg-client.ts

const { Client } = require('pg');

start();

async function start() {
    const conString = "postgres://postgres:(password)@localhost:5432/postgres";

    const client = new Client(conString);
    await client.connect();

    const query = await client.query('select 1');

    query.rows.forEach(value => console.info(`row value`, value));

    await client.end();
}

Since we're using Docker, let's build and run the image

docker build -t my-image .
docker run -it --network=host my-image

(this runs the image with host network access, allowing you to connect to a postgres database that is running on the same machine. )

Expected Result (while inside the container):

❯ yarn install
❯ ts-node test-pg-client.ts
row value { '?column?': 1 }

Actual Result:

❯ ts-node test-pg-client.ts

(no errors, no messages, just nothing)

The behavior is remedied by changing the first line in Dockerfile to the following:

FROM node:12.18.3-alpine3.12 as builder

This behavior seems similar to #2180 but wasn't totally sure if the circumstances were the same, so chose to make a new issue just in case.

@krishchow
Copy link

+1 I noticed this too, reverting to nodev12.18.3 fixed the issue for me as well

@charmander
Copy link
Collaborator

Your package.json has "pg": "^8.3.2", but can you make sure that it’s what’s being used? In the same script, try

console.log(require('pg/package.json').version);

@martinbliss
Copy link
Author

Your package.json has "pg": "^8.3.2", but can you make sure that it’s what’s being used? In the same script, try

console.log(require('pg/package.json').version);

Adding this line to the above TS file:

console.log('PG VERSION', require('pg/package.json').version);

yields the following output:

PG VERSION 8.3.2

@charmander
Copy link
Collaborator

charmander commented Aug 25, 2020

How about on the current version of Node 14?

FROM node:14.8.0-alpine3.12

@martinbliss
Copy link
Author

martinbliss commented Aug 25, 2020

Same result:

> node -v
v14.8.0
> ts-node test-pg-client.ts
PG VERSION 8.3.2

(still unable to connect to a live postgres instance that is accessible via a different script which uses ES6 fetch to verify the instance is accessible from inside the same container.)

@rufreakde
Copy link

rufreakde commented Aug 25, 2020

I have the same issue this is blocking me as I need to upgrade to node v14.4.0.
So I cloned the repo and just run your tests but they fail on node 14. Could you please investigate? @brianc

npm run test

> [email protected] test /Users/X/git/node-postgres/packages/pg
> make test-all

***Testing optional native install***
Cannot find module 'pg-native'
Require stack:
- /Users/x/git/node-postgres/packages/pg/lib/native/client.js
- /Users/x/git/node-postgres/packages/pg/lib/native/index.js
- /Users/x/git/node-postgres/packages/pg/lib/index.js
- /Users/x/git/node-postgres/packages/pg/test/native/missing-native.js
configuration-tests.js.
startup-tests.js.......
inbound-parser-tests.js..............................................................
error-tests.js
  connection emits stream errors ✔
  connection emits ECONNRESET errors during normal operation ✔
  connection does not emit ECONNRESET errors during disconnect ✔
  connection does not emit ECONNRESET errors during disconnect also when using SSL ✔
  connection emits an error when SSL is not supported ✔
  connection emits an error when postmaster responds to SSL negotiation packet ✔

utils-tests.js.........................
sasl-scram-tests.js................
result-metadata-tests.js.......
simple-query-tests.js..............?.....
throw-in-type-parser-tests.js
  emits error ✔
  calls callback with error ✔
  rejects promise with error ✔

early-disconnect-tests.js
escape-tests.js................
md5-password-tests.js....
configuration-tests.js............
query-queue-tests.js.........
notification-tests.js.
cleartext-password-tests.js..
set-keepalives-tests.js
  setting keep alive ✔

stream-and-query-error-interaction-tests.js.
prepared-statement-tests.js...........
environment-variable-tests.js
  ConnectionParameters initialized from environment variables ✔
  ConnectionParameters initialized from mix ✔
  connection string parsing ✔
  connection string parsing - ssl ✔
  ssl is false by default ✔
  ssl is false when $PGSSLMODE= ✔
  ssl is false when $PGSSLMODE=disable ✔
  ssl is false when $PGSSLMODE=allow ✔
  ssl is true when $PGSSLMODE=prefer ✔
  ssl is true when $PGSSLMODE=require ✔
  ssl is true when $PGSSLMODE=verify-ca ✔
  ssl is true when $PGSSLMODE=verify-full ✔
  ssl is [object Object] when $PGSSLMODE=no-verify ✔

creation-tests.js.................?.....
***Testing connection***
/Users/x/git/node-postgres/packages/pg/script/create-test-tables.js:44
    throw err
    ^

Error: connect ECONNREFUSED 127.0.0.1:5432
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 5432
}
make: *** [test-connection] Error 1
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] test: `make test-all`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/x/.npm/_logs/2020-08-25T06_30_56_178Z-debug.log // no useful info here

@rufreakde
Copy link

rufreakde commented Aug 25, 2020

Update:
I deleted my node_modules folder and changed pg version to "pg": "8.3.0", and this seems to work fine with node 14.4

@nicoburns
Copy link

I had this same issue with pg version 7 and Node 14. It may be worth noting in the README that pg v8 or upwards is required for Node 14.

@brianc
Copy link
Owner

brianc commented Aug 26, 2020

I had this same issue with pg version 7 and Node 14. It may be worth noting in the README that pg v8 or upwards is required for Node 14.

agreed - I can do this

@martinbliss
Copy link
Author

I had this same issue with pg version 7 and Node 14. It may be worth noting in the README that pg v8 or upwards is required for Node 14.

That's really strange, considering I have verified that I'm running PG 8.3.2 on Node 14 in a clean docker build (that is to say, no node_modules).

@yocontra
Copy link
Contributor

yocontra commented Aug 28, 2020

@martinbliss Working for me in node 14.9 w/ the latest pg version - prior to that I tested it on 14.0 and had the same problem. Make sure you wipe all of your node_modules folders and yarn/npm cache so that every native extension gets a full rebuild. Your solution will probably just be changing your docker base image to the latest 14 release instead of the first one - not sure why the first release (14.0.0) was bad, they changed a bunch of behavior with streams so it may be to blame.

@martinbliss
Copy link
Author

martinbliss commented Aug 28, 2020

@martinbliss Working for me in node 14.9 w/ the latest pg version - prior to that I tested it on 14.0 and had the same problem. Make sure you wipe all of your node_modules folders and yarn/npm cache so that every native extension gets a full rebuild. Your solution will probably just be changing your docker base image to the latest 14 release instead of the first one - not sure why the first release (14.0.0) was bad, they changed a bunch of behavior with streams so it may be to blame.

I failed to mention in my original post that I have a .dockerignore file which excludes node_modules from the COPY . . instruction, so there shouldn't be any concerns about conflicting node_modules / native binding issues with respect to node 14 vs. node 14.9 (inside a container.) If pg has an issue with certain versions of node 14, then I would expect that to be called out directly in the README. The current guidance that pg 8.3+ should work on Node 14 seems to be contradicted by this issue.

@nicoburns
Copy link

Node 14.0 made a bunch of backwards incompatible changes to streams that seemed to break quite a lot of libraries. I'd definitely avoid that version in general. They reverted the changes in 14.1, and the later releases are all fine. However, IIRC pg (once updated to v8) was not one of the libraries affected by this (at least for how we're using it).

@yocontra
Copy link
Contributor

@brianc One thing that most people in the community are doing to avoid having node versions break streams is using https://github.com/nodejs/readable-stream instead of the core stream module directly. From the node repo: "If you want to guarantee a stable streams base, regardless of what version of Node you, or the users of your libraries are using, use readable-stream only and avoid the "stream" module in Node-core"

Not even sure if this is what is responsible for this particular issue - but I do see the stream module used directly in a lot of the packages in this monorepo. We had a lot of random bugs with streams across versions (particularly 14 and early 13 releases) that disappeared by switching everything to readable-stream.

@charmander
Copy link
Collaborator

@contra The issue with pg <8.0.3 was using a property that wasn’t part of the public API. I don’t think there’s anything like that left.

@abenhamdine
Copy link
Contributor

Just adding a datapoint to this thread : we use in production pg 8.0.3 with nodejs 14.0.0 without any problem.

@charmander
Copy link
Collaborator

@martinbliss Is it also broken when connecting to a PostgreSQL server running in another container, not just the host?

@ireznik
Copy link

ireznik commented Sep 5, 2020

Thank you guys. I ran into this issue with an older version (<8) and thought I did something wrong

@martinbliss
Copy link
Author

martinbliss commented Sep 5, 2020 via email

@charmander
Copy link
Collaborator

@martinbliss Oh, I thought this was accessing a database on the host because of the --network=host. Is there any difference on a different, non-host network, then?

@martinbliss
Copy link
Author

martinbliss commented Sep 6, 2020 via email

@samueldaviddelacruz
Copy link

has this been solved for people using containers? still having the same issue using pg 8.5.X

@codyhazelwood
Copy link

@samueldaviddelacruz We are using node-Postgres now with node 14 in alpine and Debian containers. Upgrading to the latest version of node-Postgres solved the problem for us.

@samueldaviddelacruz
Copy link

samueldaviddelacruz commented Nov 21, 2020

@codyhazelwood exactly which version? im using this 14-alpine and it causing issues,pg version => "pg": "^8.5.1", this is my dockerfile:

FROM node:14-alpine
WORKDIR "/app"
COPY ./package.json ./
RUN npm install
COPY . .

CMD ["npm","run","dev"]```

@samueldaviddelacruz
Copy link

update: i fixed my issue by starting from scratch with my containers, deleted all the old images and containers and build them again, this fixed the issue on my side.

@charmander
Copy link
Collaborator

I set up a VM to reproduce the steps from the original issue exactly, and the output was:

/src # ts-node test-pg-client.js
/bin/sh: ts-node: not found

I’m not sure if that’s something that changed in a dependency, but either way:

/src # node_modules/.bin/ts-node test-pg-client.js
row value { '?column?': 1 }

@martinbliss Are you still having an issue connecting from containers with Node 14?

@SteveFromAccounting
Copy link

This was a real humdinger for me. I am using:

  • Node.js v14.15.4 (npm v6.14.10)
  • node-postgres v8.5.1

And the problem is now fixed. I was previously on 7.12.1 and same version of Node.

@Fortegot
Copy link

I've noticed this too... I cant tell the huge amount of hours i've wasted trying to make my app work with node 14...
Updating pg libraries to 8 version i could make it work again (with some further changes). Thank you all for giving the idea of upgrading pg...
But finally, i found out that a listener build on pg-pubsub has stopped working too and it seems to be no solution yet for that.
Can anyone give a piece of advice about pg-pubsub with node-14??

Thanks in advance!

@charmander
Copy link
Collaborator

@martinbliss Are you still having an issue connecting from containers with Node 14?

reopen if so!

@tkssharma
Copy link

This is why I don't like the Node JS Ecosystem
I spent 1 hour checking why my connection is not done with Postgres !!
As it was silent no errors!

@BlackBo6
Copy link

BlackBo6 commented Jun 4, 2021

Update:
I deleted my node_modules folder and changed pg version to "pg": "8.3.0", and this seems to work fine with node 14.4

yeah same , I uninstall pg and install pg version (^8.6.0) , and it works for me with node 15.14.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests