node-postgres: pg client fails to connect (silently) using Node 14
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.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 26
- Comments: 31 (4 by maintainers)
+1 I noticed this too, reverting to nodev12.18.3 fixed the issue for me as well
agreed - I can do this
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!
This was a real humdinger for me. I am using:
And the problem is now fixed. I was previously on 7.12.1 and same version of Node.
@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.
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
has this been solved for people using containers? still having the same issue using pg 8.5.X
@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 toreadable-stream
.