Home > Mobile >  Error: connect ECONNREFUSED 127.0.0.1:5432 at TCPConnectWrap.afterConnect [as oncomplete]
Error: connect ECONNREFUSED 127.0.0.1:5432 at TCPConnectWrap.afterConnect [as oncomplete]

Time:12-19

I have a simple node application with postgres database that is running perfectly on my local Machin , i used to deploy backend application on Heroku,Since it has no more free services i tried many different alternative Like {Cyclic ,RailWay} , for now i couldn't deploy the server correctly with this Error :

original: Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
}

this is my code :

"use strict";
require('dotenv').config();
const Collection = require("./collection");

const Users = require("./user.model");
const Records =require('./records')

const POSTGRES_URI = process.env.NODE_ENV === 'test' ? 'sqlite:memory:' : process.env.DATABASE_URL;
const {
Sequelize,
DataTypes
} = require("sequelize");

let sequelizeOptions =process.env.NODE_ENV === "production" ?
{
dialect: 'postgres',
protocol: 'postgres',

    } : {};

let sequelize = new Sequelize(POSTGRES_URI,sequelizeOptions);
const users = Users(sequelize, DataTypes);
const records = Records(sequelize, DataTypes);

// Users.hasMany(Records)
users.hasMany(records, {
foreignKey: "userId",
sourceKey: "id",
onDelete:'cascade'
});

records.belongsTo(users, {
foreignKey: "userId",
targetKey: "id",
});

module.exports = {
db: sequelize,
records: new Collection(records),
users:users,
};

this is .env file

DATABASE_URL=postgres://mohammadsh:0000@localhost:5432/covid
PORT=3000
SECRET=secretstring
NODE_ENV=production


this my package.json


"scripts": {
    "start": "NODE_ENV=production node index.js",
    "dev": "NODE_ENV=development node index.js"
    

  
  "dependencies": {
    "axios": "^1.2.1",
    "base-64": "^1.0.0",
    "bcrypt": "^5.1.0",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "jsonwebtoken": "^8.5.1",
    "pg": "^8.8.0",
    "sequelize": "^6.27.0",
    "sequelize-cli": "^6.5.2",
    "sqlite3": "^5.1.4"
  }

i set the .env variables with the deployment on{Cyclic and RailWay} as they are in my .env file

  • when this error happen on my local machin i just run the postgres server by this command
pg_ctl -D /home/linuxbrew/.linuxbrew/var/postgresql@14 start

how to do this on Cyclic ? or any other application

  • i tried punch of code by playing with connection options *aslo i tried to change the IP address to be 0.0.0.0 and i got the same error

original: Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
}

any help ???

CodePudding user response:

Your postgres is running localy. You can reach postgres localy with host: localhost (127.0.0.1) an on port 5432.

If you installed postgres directly on your host, or you did it with docker, mapping the port, it will work localy.

As soon you deploy it, you need to specify the correct url, because localhost (127.0.0.1) refers now to your api container, where postgres is not installed.

So if you have an external postgresDb , you need to put your credentials, url, DBname, in the connection string.

  • Related