question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pg client fails to connect (silently) using Node 14

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:26
  • Comments:31 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
krishchowcommented, Aug 24, 2020

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

10reactions
brianccommented, 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found