ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432
at Client._connectionCallback (/home/gene/capital_greek_project/api/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:131:24)
at Client._handleErrorWhileConnecting (/home/gene/capital_greek_project/api/node_modules/pg/lib/client.js:305:19)
at Client._handleErrorEvent (/home/gene/capital_greek_project/api/node_modules/pg/lib/client.js:315:19)
at Connection.emit (node:events:527:28)
at Socket.reportStreamError (/home/gene/capital_greek_project/api/node_modules/pg/lib/connection.js:52:12)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
parent: Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
I've been getting this error and I've been having a hard to time trying to connect to postgres using node js. I'm using Sequelize to define my models, I feel like it has something to do with my Ip address or my local host machine but I'm not sure how to navigate to it. Do I need to install postgres on to my machine ?
//*db.config.js
module.exports = {
HOST: 'localhost',
USER: 'postgres',
PASSWORD: '123',
DB: 'capital-greek',
dialect: 'postgres',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000,
},
};
//server.js*
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
let corsOptions = {
origin: 'http://localhost:8081',
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// database
const db = require('./app/models');
const Role = db.role;
// db.sequelize.sync()
// force: true will drop the table if it already exists
db.sequelize.sync({ force: true }).then(() => {
console.log('Drop and Resync Database with { force: true }');
initial();
});
// simple route
app.get('/', (req, res) => {
res.json({ message: 'Welcome to bezkoder application.' });
});
// routes
require('./app/routes/auth.routes.js')(app);
require('./app/routes/users.routes.js')(app);
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
function initial() {
Role.create({
id: 1,
name: 'user',
});
Role.create({
id: 2,
name: 'moderator',
});
Role.create({
id: 3,
name: 'admin',
});
}
//models/index.js
const config = require('../config/db.config.js');
const Sequelize = require('sequelize');
const sequelize = new Sequelize(config.DB, config.USER, config.PASSWORD, {
host: config.HOST,
dialect: config.dialect,
operatorsAliases: 0,
pool: {
max: config.pool.max,
min: config.pool.min,
acquire: config.pool.acquire,
idle: config.pool.idle,
},
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.user = require('../models/user.model.js')(sequelize, Sequelize);
db.role = require('../models/role.model.js')(sequelize, Sequelize);
db.role.belongsToMany(db.user, {
through: 'user_roles',
foreignKey: 'roleId',
otherKey: 'userId',
});
db.user.belongsToMany(db.role, {
through: 'user_roles',
foreignKey: 'userId',
otherKey: 'roleId',
});
db.ROLES = ['user', 'admin', 'moderator'];
module.exports = db;
CodePudding user response:
Are you sure that your Postgres server allows for connection from your IP address?
Try modifying your's pg_hba.conf
file by adding following lines:
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
host all all 10.16.120.245/24 scram-sha-256
host all all 0.0.0.0/0 trust
host all all 0.0.0.0/0 md5
Section for this should be located near 86 line
You could find this file under:
C:\Program Files\PostgreSQL\(your postgres version)\data
There is possibility that your configuration disallows connection from 127.0.0.1
(localhost).
And I'm guessing that from this error description:
Error: connect ECONNREFUSED 127.0.0.1:5432
CodePudding user response:
I finally found a solution that worked for me from another post, here is the link https://stackoverflow.com/a/31647044/18759005 . The problem was my postgres server was down. I have another solution to get the postgres server running which is from this link https://stackoverflow.com/a/49860500/18759005 , I only implemented step 1 & 2 . Both of these links are from the same question that is similar to mine.