connecting to postgres via ssh on nodejs - node.js

I have a connection on DBeaver using an ssh tunnel as follows:
sshHostname;
sshPort;
sshUser;
sshPassword;
on the actual connection to the database I have:
dbHost;
dbPort;
dbName;
dbUsername;
dbPassword;
my node js code looks something like this:
const { Pool, Client } = require('pg')
const ssh2 = require('ssh2');
const dbServer = {
host: dbHost,
port: dbPort,
database: dbName,
username: dbUser,
password: dbPassword
}
var c = new ssh2();
c.connect({
host: sshHostname,
port: 22,
username: sshUser,
password: sshPassword
});
c.on('ready', function () {
c.forwardOut(sshHostname, '22', dbHost, dbPort , function(err, data) {
const client = new Client({
host: 'localhost',
port: dbPort,
database: dbName,
user: dbUser,
password: dbPassword,
})
client.connect(function (err) {
if (err) {console.log(err)}
else {console.log('connected...')}
});
client.end();
})
});
I get the following error:
Error: connect ECONNREFUSED 127.0.0.1:dbPort
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1133:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: dbPort
}
I tried various configurations and various libraries with no success.
do you have any idea how to connect via nodejs to a database over a tunnel?
I have a feeling that I am actually not connecting to the ssh tunnel.

It could various of reasons why there is a connection refusion.
When making connections via SSH, you should have a SSH key ready on your computer and make it a part of your SSH white list. This is a common error many developers may run into, but since your critical information is hidden, we may need you to provide more details in that regard.

Related

How to make a SSH to postgres server with help of ssh2 library and query using pg libary?

I am trying to make a connection to Postgres remote host using the SSH tunnel from node application(library:ssh2) and query the data(lib:pg).
Before I used mysql2(node library) make a tunnel using ssh2(node library) and connected to Mysql remote host as well as able to query the data. But I want to do the same in Postgres remote host I am not able to connect to it.
pg Client obj config does not support the stream from ssh2 forwardOut..!
Or, Is there any library available to make this happen instead of that ssh2, pg
PS: In mysql2 when I try to make a connection with a mysql config with stream: stream key value.
var Clientssh = require('ssh2').Client;
var conn = new Clientssh();
conn.on('ready', function() {
console.log('Client :: ready');
conn.forwardOut(
'127.0.0.1',
5434,
'localhost',
5432,
function(err, stream) {
if (err) throw err;
let conf = {
host: 'localhost',
port: 5434,
user: 'test',
database: 'test',
password: 'test'
}
let remoteConnection = new Client(conf);
remoteConnection.connect(function(err) {
if (err) {
console.log(err);
console.log("Unable to connect to postgre");
res.send(err);
} else {
remoteConnection.query('SELECT * FROM test', function(err, testResult) {
remoteConnection.end();
if (err) {
console.log("Unable to fetch data");
res.send(err);
} else {
console.log("Succcess");
res.send(testResult);
}
});
}
});
});
}).connect({
host: 'hostaddress',
port: 'hostport',
username: 'hostusername',
privateKey: require('fs').readFileSync('path/for/key')
});
It shows the Connection terminated unexpectedly from this line
Client :: ready
Error: Connection terminated unexpectedly
at Connection.con.once (/node-postgres/node_modules/pg/lib/client.js:235:9)
at Object.onceWrapper (events.js:286:20)
at Connection.emit (events.js:198:13)
at Channel.<anonymous> (/node-postgres/node_modules/pg/lib/connection.js:131:10)
at Channel.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1129:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Unable to connect to postgre

orientjs not using the servers list?

hi I am developing an application in nodejs which uses orientdb.
I am using orientjs version 2.2.10
var OrientDB = require('orientjs');
var server = OrientDB({
host: '127.0.0.1',
port: 2424,
username: 'root',
password: '123',
servers: [{
host: "192.168.0.159",
port: 2425
}],
});
server.connect().then(() => {
console.log("connected");
}).catch(err => {
console.log(err);
})
above is a simple code to check whether the driver connects to the server. however when there is no orientdb instance running in localhost i get the connection refused error. it looks like that when connecting it first tries host in main object and never tries to connect to hosts in servers array. I have also tried orientjs v3, result is the same.
can someone tell me what could be the issue here?
thanks
You can try like this:
databaseConfig = {
"host": '127.0.0.1',
"port": 2424,
"username": 'root',
"password": 132,
"name": 'databaseName',
"user": 'root'
};
let server = OrientDB(databaseConfig);
let db = server.use(databaseConfig);
db.open()
.then(()=>{
console.log('Connected!')
});

How to connect KnexJS with database Oracle?

I have connection but i do not know if it exists in connection the key odbc... but even if i throw it away the error informed below persists
const knex = require('knex');
// connection database
const dbmdlog = knex({
client: 'oracle',
connection: {
host: 'localhost',
user: 'root',
password: 'root',
database: 'mydb',
odbc: 'MYDB'
}
});
module.exports = dbmdlogp;
I'm doing a select simple in a collun
But always return error:
Unhandled rejection TypeError: _this2.driver.connect is not a function
at /var/www/html/myapp/node_modules/knex/lib/dialects/oracle/index.js:143:21
at Promise._execute (/var/www/html/myapp/node_modules/bluebird/js/release/debuggability.js:299:9)
at Promise._resolveFromExecutor (/var/www/html/myapp/node_modules/bluebird/js/release/promise.js:481:18)
at new Promise (/var/www/html/myapp/node_modules/bluebird/js/release/promise.js:77:14)
at Client_Oracle.acquireRawConnection (/var/www/html/myapp/node_modules/knex/lib/dialects/oracle/index.js:142:12)
at Object.create (/var/www/html/myapp/node_modules/knex/lib/client.js:231:16)
at Pool._createResource (/var/www/html/myapp/node_modules/generic-pool/lib/generic-pool.js:326:17)
at Pool.dispense [as _dispense] (/var/www/html/myapp/node_modules/generic-pool/lib/generic-pool.js:314:12)
at Pool.acquire (/var/www/html/myapp/node_modules/generic-pool/lib/generic-pool.js:392:8)
at /var/www/html/myapp/node_modules/knex/lib/client.js:281:19
at Promise._execute (/var/www/html/myapp/node_modules/bluebird/js/release/debuggability.js:299:9)
at Promise._resolveFromExecutor (/var/www/html/myapp/node_modules/bluebird/js/release/promise.js:481:18)
at new Promise (/var/www/html/myapp/node_modules/bluebird/js/release/promise.js:77:14)
at Client_Oracle.acquireConnection (/var/www/html/myapp/node_modules/knex/lib/client.js:272:12)
at /var/www/html/myapp/node_modules/knex/lib/runner.js:200:30
at Promise._execute (/var/www/html/myapp/node_modules/bluebird/js/release/debuggability.js:299:9)
A combination of knex and OracleDB works fine. Here are packages from package.json: "knex": "0.13.0", "oracledb": "1.13.1"
And the knex call:
var conn = knex({
client: 'oracledb',
connection: {
host: config.oracle.host,
user: config.oracle.user,
password: config.oracle.password,
database: config.oracle.database,
}
});
In case someone needs to pass a connection string. Please note however, the details inside the connection string will need to match your own environment.
const oracledb = require("oracledb");
oracledb.initOracleClient({ libDir: "C:\\oracle\\instantclient_12_1" });
const knex = require("knex")({
client: "oracledb",
connection: {
user: "YOUR_USER",
password: "YOUR_PASSWORD",
connectString: "(DESCRIPTION=(CONNECT_TIMEOUT=10)(RETRY_COUNT=3)(SOURCE_ROUTE=yes)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=tcp)(HOST=your2.domain.com)(PORT=11529))(ADDRESS=(PROTOCOL=tcp)(HOST=your2.domain.com)(PORT=11529)))(ADDRESS_LIST=(FAILOVER=on)(LOAD_BALANCE=off)(ADDRESS=(PROTOCOL=tcp)(HOST=your3.domain.com)(port=1521))(ADDRESS=(PROTOCOL=tcp)(HOST=your4.domain.com)(port=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=YOUR_SERVICE.your5.domain.com)))",
requestTimeout: 100
},
fetchAsString: ["number", "clob"]
});
knex.select().from("TABLE1").asCallback(function(err, rows){
if(err)
console.log(err);
else
console.table(rows);
});

nodejs with node-mysql remote connection

I have an issue that maybe someone can help me with:
I'm trying to build a nodeJS server, working from my laptop, using node-mysql, and trying to make a remote connection to my database.
function foo (callback) {
// Connect to the database
var mysql = require('mysql');
var connection = mysql.createConnection({
host: <ipaddress>,
user: "test_developer",
password: "test_developer",
database: "test3"
});
connection.connect(function(err){
if (err){
console.log("cannot connect to database");
console.log(err);
}
else
{
console.log("connected to database");
}
});
};
// Implementation
foo(function(err, result){
console.log("Never made it here");
});
This times out with:
cannot connect to database
{ [Error: connect ETIMEDOUT]
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
syscall: 'connect',
fatal: true
}
I believe the database is ok, I can connect with phpMyAdmin from browser.
What am I doing wrong?

getaddrinfo ENOTFOUND when using tedious in node.js

I'm trying to connect to a local SQL Express server using Tedious but keep getting
failed Error: getaddrinfo ENOTFOUND
Am I using the wrong address here?
var Connection = require('tedious').Connection;
var config = {
userName: 'sa',
password: 'mypassword',
server: 'LOCALHOST\\SQLEXPRESS',
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to go...
if(err) {
console.log(err);
return;
}
executeStatement();
}
);
"Microsoft style strings of hostname\instancename are not supported."
- pekim
I posted the same issue on github and here is the full answer: https://github.com/pekim/tedious/issues/118
Just as #Cotten said, but here is an example.
TCP/IP connection must be enabled and the port shouldn't be included within the server string, it must go inside the configuration as a numeric value.
var config = {
server: 'your_ip_address',
authentication: {
type: 'default',
options: {
userName: 'your_username',
password: 'your_password'
}
},
options: {
database: 'your_database',
port: 1234 //your port number
}
};

Resources