pg client fails to connect (silently) using Node 14
See original GitHub issueThe 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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:26
- Comments:31 (4 by maintainers)
Top Results From Across the Web
Why is node-postgres (pg) hanging when I call client.connect()?
I got v7. 18.2 from running npm install pg. Seems that doesn't always install the latest version. npm install pg@latest does the trick....
Read more >Process | Node.js v19.3.0 Documentation
This is useful for tracking potential errors in an application while using the Promise constructor, as multiple resolutions are silently swallowed.
Read more >34.1. Database Connection Control Functions - PostgreSQL
This function will close the connection to the server and attempt to establish a new connection, using all the same parameters previously used....
Read more >A Comprehensive Guide To Error Handling In Node.js
js makes heavy use of callback functions for much of its error handling. A callback function is passed as an argument to another...
Read more >Let It Crash: Best Practices for Handling Node.js Errors on ...
Some strategies to gracefully shutdown the Node.js process and quickly restart your application after a catastrophic error terminates your ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+1 I noticed this too, reverting to nodev12.18.3 fixed the issue for me as well
agreed - I can do this