Knex.js won't connect to postgres - node.js

I'm trying to connect to a PostgreSQL database using Knex.js, but I just can't get a connection to happen. The only exception I'm seeing is:
Error KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
I built this simple test script to make sure it wasn't part of our program:
const knex = require("knex")({
client: 'pg',
connection: {
host : 'localhost',
port: 5432,
database: 'postgres',
user: 'postgres',
password: 'password'
},
pool: {
afterCreate: (conn, done) => {
console.log("Pool created");
done(false, conn);
}
},
debug: true,
acquireConnectionTimeout: 2000
});
console.log("A")
const a = knex.raw('select 1+1 as result').then(result => console.log("A Success", result)).catch(err => console.log("A Error", err));
console.log("B")
const b = knex.select("thing").from("testdata").then(data => console.log("B Success", data)).catch(err => console.log("B Error", err));
console.log("C")
const c = knex.transaction(trx => {
trx.select("thing").from("testdata")
.then(data => {
console.log("C Success", data);
})
.catch(err => {
console.log("C Error", err);
});
})
.catch(err => {
console.log("C Error", err);
});
console.log("waiting on query")
// Promise.all([a, b, c]).then(() => {
// console.log("Destroying")
// knex.destroy()
// })
Which is producing the following output
A
B
C
waiting on query
A Error KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
at Client_PG.acquireConnection (E:\DEV\work\niba-backend\node_modules\knex\lib\client.js:347:26)
at runNextTicks (internal/process/task_queues.js:58:5)
at listOnTimeout (internal/timers.js:520:9)
at processTimers (internal/timers.js:494:7) {
sql: 'select 1+1 as result',
bindings: undefined
}
B Error KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
at Client_PG.acquireConnection (E:\DEV\work\niba-backend\node_modules\knex\lib\client.js:347:26) {
sql: undefined,
bindings: undefined
}
C Error KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
at Client_PG.acquireConnection (E:\DEV\work\niba-backend\node_modules\knex\lib\client.js:347:26)
at async Transaction.acquireConnection (E:\DEV\work\niba-backend\node_modules\knex\lib\transaction.js:213:28)
It's never calling the afterCreate method. I have tried it against both our dev database using settings that work for everyone else, and a locally running postgres installation with every combination of settings I could come up with. I even passed this off to another member of the team and it worked fine, so there's something up in my machine, but I have no idea what could be blocking it. I'm not seeing any connection attempts in the postgres logs, and I can't seem to get any better error messages to work off of.
If anyone could come up with things I can try, or ways to get more information out of Knex I would really appreciate it.

I traced the issue down to the verion of the 'pg' package we were using. Was using 7.18 and when I upgraded to the latest version (8.4) it started connecting. No idea why the 7.x version wasn't working.

Related

Failed to connect to mydb.database.windows.net:1433 in 15000ms (Microsoft Azure SQL Database)

I am able to retrieve data from Microsoft Azure SQL Database using below code: -
const sql = require("mssql");
var config = {
user: "user_name",
password: "Pass#1234",
server: "mydb.database.windows.net",
database: "db_name",
options: {
enableArithAbort: true,
},
stream: true,
};
module.exports = function getQueryResult(query) {
return new Promise((res, rej) => {
sql.connect(config).then((pool) => {
pool.query(query, (err, result) => {
if (err) rej(err);
res(result);
});
});
});
};
I am using getQueryResult function to get the data from database.
Everything is going perfect accept the thing that the below errors occurs in between.
Failed to connect to mydb.database.windows.net:1433 in 15000ms (Microsoft Azure SQL Database)
ConnectionError: Failed to connect to mydb.database.windows.net:1433 read ECONNRESET
ConnectionError: Failed to connect to mydb.database.windows.net:1433 socket hang up
I know this question has been asked before. But I have tried all the solutions. None of the solution was specifically for Microsoft Azure SQL Database so I thought might be there is some problem in database.
Thanks in advance.
Your code is a bit different from mine, my options is enclosed in double quotes. You also can download my sample code, it works for me, I have test it.
Tips:
You need set the rule of Firewalls. Make sure your local or webapp can access dbserver.
My code:
const sql = require('mssql')
const config = {
user: 'username',
password: 'pwd',
server: '***sqlserver.database.windows.net', // You can use 'localhost\\instance' to connect to named instance
database: 'yourdb',
"options": {
"encrypt": true,
"enableArithAbort": true
}
}
const poolPromise = new sql.ConnectionPool(config)
.connect()
.then(pool => {
console.log('Connected to MSSQL')
return pool
})
.catch(err => console.log('Database Connection Failed! Bad Config: ', err))
module.exports = {
sql, poolPromise
}

Connection to postgresql db from node js

I'm tyring to make a connection from my nodejs script to my db connection, but seems like there is a suspicius issue i'm not able to figure out.
At the moment, this is my code:
const { Pool } = require('pg');
const pool = new Pool({
user: 'user',
host: '192.168.1.xxx',
database: 'database',
password: 'password',
port: 5432,
});
pool.on('error', (err, client) => {
console.error('Error:', err);
});
const query = `SELECT * FROM users`;
pool.connect()
.then((client) => {
client.query(query)
.then(res => {
for (let row of res.rows) {
console.log(row);
}
})
.catch(err => {
console.error(err);
});
})
.catch(err => {
console.error(err);
});
The issue seems to be in pool.connect(), but i can't understand what i'm missing because i got no errors in the log. I've installed pg module in the directory of my project with npm install --prefix pg and i know modules are loaded correctly.
I edited postgresql.conf:
# - Connection Settings -
listen_addresses = '*'
and pg_hba.conf
host database user 192.168.1.0/24 md5
to make the database reachable via lan and seems liek it works, because i'm able to connect successfully with apps like DBeaver...but i can't with NodeJS.
It's possible there is some kind of configuration i've to active?

Exception handling not working for PG npm package

I installed "pg": "^8.0.2" and created the database.js file with database credentials. But no matter what go wrong it never enters in the catch block to show error. Instead it always logs connected to the database. Can anyone point out what I'm doing wrong. Thank You!
Database.js
const Pool = require('pg').Pool;
const pool = new Pool({
user: 'roothjk',
host: 'localhost',
database: 'sf',
password: 'admin',
port: 5432
});
try {
pool.connect()
console.log('connected to the db');
} catch (e) {
console.log('Error connecting to db');
}
connect returns a Promise, and then you move to the next statement. Instead, you should use the then and cath methods:
pool.connect()
.then(c => console.log('connected to the db'))
.catch(e => console.log('Error connecting to db'));

Sequelize connection and logging issues

I am trying to use Sequelize to connect with a SQL Server 2012 database. When my connection string was clearly wrong, I was seeing ECONN REFUSED messages and timeout. Now, I am not getting any response, despite logging on success and fail, per this code:
import * as Sequelize from 'sequelize';
-- connection string redacted
let seqConn = new Sequelize("mssql://**;Initial Catalog=**;Persist Security Info=True;Integrated Security=SSPI; User ID=**; Password=**")
seqConn
.authenticate()
.then(function(err) {
console.log('Connection has been established successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the database:', err);
});
I was previously using the syntax:
let seqConn = new Sequelize("DB", "Username", "Password",
{
dialect: 'mssql',
dialectOptions: {
instanceName: 'dev'
},
host: '**'
};
But I couldn't find a setting for integrated security or other fancy SQL Server things.
Regardless, my current connection string isn't erroring. But also, it is not saying the connection was established.
I tried passing my seqConn to a model to use it to retrieve a model:
getModel(seqConn): void {
console.log("Getting");
var model = seqConn.define("dbo.Model", {
modelID: Sequelize.INTEGER,
modelNo: Sequelize.INTEGER,
modelName: Sequelize.STRING(50),
modelAge: Sequelize.DATE
});
seqConn.sync().then(function() {
model.findOne()
.then( function (modelRes){
console.log("got a result!?");
console.log(modelRes.get("modelName"));
})
.catch( function (error){
console.log("it didn't work :( ");
});
});
console.log('after getter');
return;
}
I'm not confident that this is the right way to use sequelize, and I'm still establishing my project structure, but for now I expect to either see a "got a result" or an error, but nothing is logging here from either the then() or the catch(). The before/after getter logs are logging fine.
I have considered that it takes a long time to connect, but my IT says that he saw my successful connection in our SQL logs, and previously I was getting timeouts in about 15,000 ms, I think.
The problem was solved by including another NPM module: `sequelize-msnodesqlv8'
And then the code to connect to Sequelize looked like:
var Sequelize = require('sequelize');
var seqConn = new Sequelize({
dialect: 'mssql',
dialectModulePath: 'sequelize-msnodesqlv8',
dialectOptions: {
connectionString: 'Driver={SQL Server Native Client 11.0};[REDACTED]'
}
});
seqConn
.authenticate()
.then(function(err) {
console.log('Connection has been established successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the database:', err);
});

how to catch Sequelize connection error

How to catch a sequelize connection error in case there is one?
I tried to do
var connection = new Sequelize("db://uri");
connection.on("error", function() { /* perhaps reconnect here */ });
but apparently this is not supported.
I wanted to do this because I think sequelize might be throwing an occasional unhandled ETIMEOUT and crashing my node process.
Currently I am using sequelize to connect a mysql instance. I only need it for like 2-3 hours and during that time I will be doing a many read queries. The mysql server will not be connected to anything else during that time.
Using sync() for this is considerably dangerous when using external migrations or when database integrity is paramount (when isn't it!)
The more up-to-date way of doing this is to use authenticate()
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
Using sequelize sync method provides an easy way to catch the error.
The then block handles a Successful connection and the catch handles the rejection.To get a detailed reason for a failure access the error object.
example: error.message e.t.c
Hope this helps.
sequelize.sync().
then(function() {
console.log('DB connection sucessful.');
}).catch(err=> console.log('error has occured'));
Use sequelize sync for that
var Sequelize = require('sequelize');
sequelize = new Sequelize(config.database, config.username, config.password, {
'host' : config.host,
'dialect' : config.dialect,
'port' : config.port,
'logging' : false
})
sequelize.sync().then(function(){
console.log('DB connection sucessful.');
}, function(err){
// catch error here
console.log(err);
});

Resources