I am trying to connect to my local postgres db with knex but I keep getting this error.
{ Error: connect ECONNREFUSED 127.0.0.1:5432
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432 }
// Here is how everything is set up:
const connection = require('./db/knexfile.js').development;
const knex = require('knex')(connection);
const app = express();
const server = app.listen(PORT, '127.0.0.1', 'localhost', () => console.log(`Listening on ${ PORT }`));
// Inside knexfile.js
module.exports = {
development: {
client: 'pg',
connection: {
"user": "development",
"password": "development",
"database": "testdb",
"host": "127.0.0.1",
"port": 5432
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}
};
Additional information:
I am also running a React front end through webpack on localhost:3000 that sends http requests to 8080 through a proxy setting on the webpack server but this seems to be working.
I also have elasticsearch running on localhost:9200 and that seems to be wroking, too.
The issue was that I had an incorrect version of postgres installed. I reinstalled postgres and no longer had the issue.
Another, faster, approach is to connect to a local database is to use AF_UNIX sockets. You will have to give access rights to the Linux user that will host the application:
development:
connection: {
host: '/var/run/postgresql', // On Debian it is this one, check your distro
database: 'database_name'
},
This kind of access was measured to be 30% faster than going through AF_INET (TCP/IP) sockets.
Here an old article about that: Performance of UNIX sockets vs TCP sockets
Related
I'm trying to make a request to postgreSQL from a Node and Express server on localhost. But I keep getting this error Error: connect ECONNREFUSED 127.0.0.1:5432 when trying to open localhost:8000/todos - not sure why.. I changed the username to 'postgres' (which is the defualt username), password, host, database name and port are all correct.. I can check the todos using SELECT * FROM todos; however, the app just breaks and gives me that code. I made sure that the server is running. I started it in services, and I can check the content of todos (which I manually added) in the SQL shell. Any ideas on why this is happening? I've gone thru all the other pages with similar issue, but I'm pretty new to postgreSQL overall and I'm unable to find an answer. I'm not using docker.
this is the exact error in the console:
Server runnnig on PORT 8080
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
main server.js
const PORT = 8080;
const express = require("express");
const app = express();
const pool = require("./db");
app.get("/todos", async (req, res) => {
try {
const todos = await pool.query("SELECT * FROM todos");
res.json(todos.rows);
} catch (err) {
console.error(err);
}
});
app.listen(PORT, () => console.log(`Server runnnig on PORT ${PORT}`));
db.js file where the connection is coming from
const Pool = require("pg").Pool;
require("dotenv").config();
const pool = new Pool({
user: "postgres",
password: process.env.PASSWORD,
host: "localhost",
port: 5432,
database: "todoapp",
});
module.exports = pool;
I'm having a strange problem when making a request
Error log AWS
AxiosError: connect ECONNREFUSED 127.0.0.1:80
2022-12-15T12:46:48.592+02:00
Copy
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16) {
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16) {
2022-12-15T12:46:48.592+02:00 port: 80,
2022-12-15T12:46:48.592+02:00 address: '127.0.0.1',
2022-12-15T12:46:48.592+02:00
Copy
syscall: 'connect',
syscall: 'connect',
2022-12-15T12:46:48.592+02:00 code: 'ECONNREFUSED',
Everything goes ok on the local server, but on the aws server I can't make a request in an external endpoint
await axios({
method: 'post',
url,
auth: {
"username": process.env.ENV == 'Production' ? `${process.env.prod_url}` : `${process.env.dev_url}`,
"password": process.env.ENV == 'Production' ? `${process.env.prod_pass}` : `${process.env.dev_pass}`
},
data: {
"root": {
"head": {
"auth": {
"username": process.env.ENV == 'Production' ? `${process.env.prod_url}` : `${process.env.dev_url}`,
"password": process.env.ENV == 'Production' ? `${process.env.prod_pass}` : `${process.env.dev_pass}`
},
"service": service
},
"main": mainJSON
}
},
httpsAgent: new Agent({
rejectUnauthorized: false,
})
})
The configuration is on a docker image in node.. So I don't know what it could be
If I make a request in my database everything is ok, but only when I make a request with axios in another endpoint, this happens
It looks like you are trying to connect to localhost on port 80. If that is the intended target, ensure that you have a web server running on that port.
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateWebServer.html
If localhost is not your intended target, change the url to the external one which you are trying to reach ensuring your security groups allow traffic on that port.
I have created a database in GCP but when I try to connect it with my Node.js server on localhost I'm getting the following error:
original: Error: connect ETIMEDOUT 35.202.153.108:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '35.202.153.108',
port: 5432
}
Here is my code in db.js:
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(
process.env.DATABASE_NAME,
process.env.DATABASE_USERNAME,
process.env.DATABASE_PASSWORD,
{
host: process.env.DATABASE_HOST,//35.202.153.108
dialect: 'postgres',
}
);
module.exports = sequelize;
db.authenticate()
.then((t) => console.log('Connection has been established successfully.'))
.catch((error) => console.error('Unable to connect to the database:', error));
Can someone help me with connecting to GCP database?
Try this one:
Create a TCP connection by using Node.js
const createTcpPool = config => {
// Extract host and port from socket address
const dbSocketAddr = process.env.DB_HOST.split(':'); // e.g. '127.0.0.1:5432'
// Establish a connection to the database
return Knex({
client: 'pg',
connection: {
user: process.env.DB_USER, // e.g. 'my-user'
password: process.env.DB_PASS, // e.g. 'my-user-password'
database: process.env.DB_NAME, // e.g. 'my-database'
host: dbSocketAddr[0], // e.g. '127.0.0.1'
port: dbSocketAddr[1], // e.g. '5432'
},
// ... Specify additional properties here.
...config,
});
};
The link as well content several other examples for TCP, Socket, SQL and even how to retry connections to Cloud SQL for Postgres in Google Cloud using node.js, php and some others.
You need to use Cloud SQL Proxy to connect to your Cloud SQL from localhost. Then your app connects with the Cloud SQL Proxy with host name=localhost.
Basically, after setting up Cloud SQl Proxy, your app acts like the database is hosted locally but it is actually talking with the Proxy and Proxy is talking with your Cloud SQL instance.
I can't connect with database using Sequelize, code seems to be correct. MAMP server is running and connected to mySQL workbench. There is created farmerdb. mysql2 is installed and listed in package.json. But still I get this error message.
SequelizeAccessDeniedError: Access denied for user 'root'#'localhost' (using password: YES)
at ConnectionManager.connect (/Users/jav/farmerAPI/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:118:17)
at process._tickCallback (internal/process/next_tick.js:68:7)
name: 'SequelizeAccessDeniedError',
parent:
{ Error: Access denied for user 'root'#'localhost' (using password: YES)
at Packet.asError (/Users/jav/farmerAPI/node_modules/mysql2/lib/packets/packet.js:712:17)
at ClientHandshake.execute (/Users/jav/farmerAPI/node_modules/mysql2/lib/commands/command.js:28:26)
at Connection.handlePacket (/Users/jav/farmerAPI/node_modules/mysql2/lib/connection.js:425:32)
at PacketParser.Connection.packetParser.p [as onPacket] (/Users/jav/farmerAPI/node_modules/mysql2/lib/connection.js:75:12)
const sequelize = new Sequelize(dbConfig.DB_NAME, dbConfig.DB_USER, dbConfig.DB_PASSWORD,{
host: dbConfig.DB_HOST,
dialect: dbConfig.dialect,
port: dbConfig.PORT,
logging: false
});
module.exports = {
DB_HOST: 'localhost',
DB_USER: 'root',
DB_PASSWORD: '12345678',
DB_NAME: 'farmerdb',
dialect : 'mysql',
PORT: 3306
}
// DB
const db = require('./models/index')
async ()=> {
try{
await db.sync()
}catch(e){
console.log(e);
}
};
MAMP server running
MySQLWorkbench connected to MAMP server. Successfully Host 127.0.0.1 Port 3306 User root.
I rented an EC2 instance of Ubuntu 16.xx on AWS and installed PostgreSQL on it. I created a database and table inside the PostgreSQL on EC2. Right now I am trying to connect to and get data from the database via a local Node.js project using knex.
I already enabled the inbound rule for port 5432 to IP from anywhere.
However, it returns error message as below:
Error: connect ECONNREFUSED 13.229.xxx.xxx:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1142:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '13.229.xxx.xxx',
port: 5432
}
How am I gonna fix it? Do I need to install and implement a reversed proxy? If so, how do I set it up? I know there is RDS on AWS, but I need to use EC2 to implement it.
Here are some of my codes:
This is the knex setting, I have pg installed. The connection to a local database is successful. But when I switch the host to whether a public IP/ private IP/ ec2-13-229-xxx-xxx.ap-southeast-1.compute.amazonaws.com. They all return the above error message.
development: {
client: 'postgresql',
connection: {
host: '13.229.xxx.xxx',
database: 'project2',
user: 'postgres',
password: 'postgres',
port: 5432,
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
},
},
This is the Node.js code that I used to connect to the server. A very simple one, just to test the connection.
const express = require('express');
const hbs = require('hbs');
const app = express();
const knexConfig = require('./knexfile')['development'];
const knex = require('knex')(knexConfig);
let query = knex.select('*').from('users');
query
.then((data) => {
console.log(data);
})
.catch((err) => console.log(err));
This is my firewall setting which is turned off
Also, I paused my Kaspersky.
This is my pg_hba.conf file
And I am not sure where to add the permission of my personal IP.
This issue was related to the pg_hba.conf being restricted to localhost only.
Additionally the postgres.conf needed to have listen_addresses = '*'.
By whitelisting outside access, it was possible to access the database.
Additional support from this article.