Struggling with connection issues with Postgres, NodeJS, and Node-Postgres

Issue

This Content is from Stack Overflow. Question asked by Vinn

I use node js and node-postgres to manage my data.

One of the most common things I do is read data from the database, and then do something with that data. Often times, I update the database based on the outcome of a specific action.

I use elephant SQL to host my DB. My connection limit is 5 and I have set set this to 5:

const pool = new Pool({
    connectionString,
    max: 5, // sets max connections
});

However, when I run scripts, I still get “too many connections”.

As a workaround, I write scripts with set interval, like this:


const { pool, client } = require("./database/pgConnect");

const main = () => {
    //
    pool
        .query(
            `
  Select * from raw_data
  WHERE website = ''
  limit 1
`
        )
        .then((data) => {
            const rows = data.rows;
            //
            client.end();
            rows.map(async (row) => {
                const id = row.id;
                const website = row.email.split("@")[1];

                await pool
                    .query(
                        `
        update raw_data
        SET website = $1
        WHERE id = $2
      `,
                        [website, id]
                    )
                    .then(() => {
                        console.log(`${website} updated...`);
                    })
            });
        });
};

setInterval(() => {
    main();
}, 2000);

Is there a better way to do this? Why am I going over the connection limit when I have set it to 5?



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?