1

I am always getting connection refused error, enter image description here

while if I connect via dbeaver its get successful. Please help me with what am I missing here. I have tried via tunnel-ssh, pg. Still same issue.

const { Client } = require('ssh2');
const postgres = require("postgres");
// SSH Configuration
const sshConfig = {
  host: '143.xx.xxx.xx',
  port: 22,
  username: 'root',
  password:"*****"
};

// PostgreSQL Database Configuration
const dbConfig = {
  user: 'postgres',
  host: 'localhost', // This is localhost because we're using an SSH tunnel
  database: 'postgres',
  password: '******',
  port: 30432, // PostgreSQL port on the remote server
};

// Create an SSH tunnel
const sshTunnel = new Client();
async function getUsersOver(sql) {
  const users = await sql`
    select
      *
    from users
  `
  // users = Result [{ name: "Walter", age: 80 }, { name: 'Murray', age: 68 }, ...]
  return users
}
sshTunnel.on('ready', () => {
  sshTunnel.forwardOut(
    'localhost', // Local address
    dbConfig.port, // Local port (automatically assigned)
    dbConfig.host, // Remote address (the database host)
    dbConfig.port, // Remote port (the PostgreSQL port on the remote server)
    (err, stream) => {
      if (err) throw err;

      // Create a PostgreSQL client connected through the SSH tunnel
      const dbClient =  postgres({...dbConfig, stream});
      getUsersOver(dbClient)
    }
  );
});

sshTunnel.connect(sshConfig);
10
  • Add the error message that you are getting Commented Oct 24, 2023 at 15:05
  • @PepeNO Have added the error screenshot. Commented Oct 24, 2023 at 15:19
  • Try establishing manually the ssh tunneling as can be seen in this question to troubleshoot the possible cause. stackoverflow.com/questions/16835761/… Commented Oct 24, 2023 at 15:49
  • Yes, I tried via dbeaver and its working from there. I am missing something here, I dont know what. @PepeNO Commented Oct 24, 2023 at 15:55
  • Did you use the options in SSH tab of Connection Settings ticking Use SSH Tunnel and filling all the corresponding fields and testing connection successfully? Commented Oct 24, 2023 at 16:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.