I'm using Vercl's serverless functions and Sequelize for postgres, i have tested the connection on my local machine using vercel dev command and everything is working fine.
but when i deploy it, the connection to postgres RDS is very unpredictable, it sometimes works but if i make two requests 1 minute apart from each other i get
ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432
at Client._connectionCallback (/var/task/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:172:24)
at Client._handleErrorWhileConnecting (/var/task/node_modules/pg/lib/client.js:305:19)
at Client._handleErrorEvent (/var/task/node_modules/pg/lib/client.js:315:19)
at Connection.emit (events.js:314:20)
at Connection.EventEmitter.emit (domain.js:483:12)
at Socket.reportStreamError (/var/task/node_modules/pg/lib/connection.js:53:12)
at Socket.emit (events.js:314:20)
at Socket.EventEmitter.emit (domain.js:483:12)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3) {
parent: Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
},
original: Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
}
i suspected this might be due to serverless nature of Vercel functions, tried to fiddle with pool connections, even tried closing and opening connections after every query but it doesn't seem to be working.
this is my connection
const pg = require('pg');
const { Sequelize } = require('sequelize');
new Sequelize(POSTGRES_DB, POSTGRES_USER_WEB, POSTGRES_USER_WEB_PASSWORD, {
host: POSTGRES_HOST, //AWS RDS HOST
dialectModule: pg,
dialect: 'postgres',
pool: {
max: 5,
min: 0,
idle: 1000,
handleDisconnects: true
}
});
this is my vercel.json
{
"version": 2,
"builds": [{ "src": "index.js", "use": "@vercel/node" }],
"rewrites": [
{ "source": "/(.*)", "destination": "index.js" },
{ "source": "/webhook", "destination": "index.js" },
{ "source": "/v1", "destination": "index.js" }
]
}
this is the error i get, i still suspect this has to do with pools but i don't know how to configure it.
[SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432
at Client._connectionCallback (/var/task/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:172:24)