node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

pg client fails to connect (silently) using Node 14

Open martinbliss opened this issue 6 years ago • 32 comments

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 https://github.com/brianc/node-postgres/issues/2180 but wasn't totally sure if the circumstances were the same, so chose to make a new issue just in case.

martinbliss avatar Aug 23 '20 17:08 martinbliss

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

krishchow avatar Aug 24 '20 01:08 krishchow

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);

charmander avatar Aug 24 '20 13:08 charmander

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

martinbliss avatar Aug 24 '20 13:08 martinbliss

How about on the current version of Node 14?

FROM node:14.8.0-alpine3.12

charmander avatar Aug 25 '20 04:08 charmander

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.)

martinbliss avatar Aug 25 '20 05:08 martinbliss

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 avatar Aug 25 '20 06:08 rufreakde

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

rufreakde avatar Aug 25 '20 07:08 rufreakde

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.

nicoburns avatar Aug 25 '20 19:08 nicoburns

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

brianc avatar Aug 26 '20 21:08 brianc

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).

martinbliss avatar Aug 26 '20 22:08 martinbliss

@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.

yocontra avatar Aug 28 '20 18:08 yocontra

@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.

martinbliss avatar Aug 28 '20 19:08 martinbliss

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).

nicoburns avatar Aug 28 '20 19:08 nicoburns

@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.

yocontra avatar Aug 28 '20 21:08 yocontra

@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.

charmander avatar Aug 28 '20 21:08 charmander

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

abenhamdine avatar Sep 02 '20 09:09 abenhamdine

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

charmander avatar Sep 03 '20 18:09 charmander

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

ireznik avatar Sep 05 '20 10:09 ireznik

I don’t know, since I only run databases in containers.

On Thu, Sep 3, 2020 at 1:18 PM Charmander [email protected] wrote:

@martinbliss https://github.com/martinbliss Is it also broken when connecting to a PostgreSQL server running in another container, not just the host?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/brianc/node-postgres/issues/2317#issuecomment-686665534, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC3FK5ZOESOMHRTBI5MACEDSD7MWRANCNFSM4QIYJ7KA .

martinbliss avatar Sep 05 '20 13:09 martinbliss

@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?

charmander avatar Sep 06 '20 04:09 charmander