Connecting to azure flexible postgres server via node pg - node.js

I am using the free subscription at Azure and have successfully created a Ubuntu Server and a Flexible Postgres Database.
Until recently I accessed the DB directly from my Windows 10 desktop. Now I want to route all access through the Ubuntu Server.
For this I have installed Open SSH Client and Open SSH Server on my Windows 10 machine and done the necessary local port forwarding with ssh -L 12345:[DB IP]:5432 my_user#[Ubuntu IP]
The connection works, I confirmed it with pgcli on my desktop with pgcli -h 127.0.0.1 -p 12345 -u my_user -d my_db
But when I am trying to connect via node-pg I receive the following error
UnhandledPromiseRejectionWarning: error: no pg_hba.conf entry for host "[Ubuntu IP]", user "my_user", database "my_db", SSL off
I have already added a Firewall Rule in Azure with the [Ubuntu IP], and the error remains. What bugs me further is that in the Azure Portal of the DB I have enabled "Allow public access from any Azure service within Azure to this server", so the extra Firewall should not even be necessary for this connection.
For the last week, I have been stuck on this and now the connection is finally established, but not accessible by my code. Pretty frustrating. I would be glad about ANY pointers on how to fix this.
Edit #1:
I can't post the pg_hba.conf file. Because the Postgres DB is managed by Azure, I do not have access to pg_hba, which makes the situation more difficult to understand.
My node.js code for testing the connection:
const pg = require("pg");
const passwd = "...";
const client = new pg.Client({
user: 'admin',
host: '127.0.0.1',
database: 'test',
password: passwd,
port: 12345
});
client.connect()
client.on('uncaughtException', function (err) {
console.error(err.stack);
});
const query = "SELECT * FROM test";
try {client.query(query, (err,res) => {
if (err) {
console.error(err);
}
console.log(res);
})}
catch (e) {
console.error(e)
}

The comment by #jjanes helped me in understanding the issue, thank you.
This edited pg.Client config solved my problem:
const client = new pg.Client({
user: 'admin',
host: '127.0.0.1',
database: 'test',
password: passwd,
port: 12345,
ssl: {rejectUnauthorized: false}
});
I found this specific SSL option here https://node-postgres.com/features/ssl

Related

Unable to connect to remote redis host [nodeJS]

const redis = require('redis');
require('dotenv').config();
console.log(process.env.redisHost, ':', process.env.redisPort);
const redisClient = redis.createClient({
host: process.env.redisHost,
port: process.env.redisPort,
password: process.env.redisKey
});
redisClient.connect();
redisClient.on('error', err => console.log('Redis error: ', err.message));
redisClient.on('connect', () => console.log('Connected to redis server'));
module.exports = redisClient;
I tried this sample from redis docs but still I'm getting an error stating:
Redis error: connect ECONNREFUSED 127.0.0.1:6379
I logged the environment host and port variables to the console and I got the remote host ipv4 address, but still the client is trying to connect to localhost instead of remote host (I purposely uninstalled redis from my local device to check if the client is working as it is supposed to). I also confirmed that the remote redis host is working perfectly.
I also tried different methods like :
https://cloud.google.com/community/tutorials/nodejs-redis-on-appengine
redis.createClient(port, host, {auth_pass: password});
But still, I got the same error.
I am able to connect to the redis host via commandline:
redis-cli.exe -h XX.XX.XX.XXX -a Password
XX.XX.XX.XXX:6379> set name dhruv
OK
XX.XX.XX.XXX:6379> get name
"dhruv"
XX.XX.XX.XXX:6379> exit
I'm trying to use redis on nodejs for the first time, so don't have a proper idea but I think I am doing everything right.
Any solution/workaround will be helpful :D
It worked with this code:
const url = `redis://${process.env.redisHost}:${process.env.redisPort}`;
const redisClient = redis.createClient({
url,
password: process.env.redisKey
});
redisClient.connect();
can you check if in the destination the port is reachable. it maybe the firewall block your access

Aurora Postgres Connection Timeout Nodejs

This was working the last time I looked at it. The code remains unchanged.
I have an SSH tunnel through an ec2 instance to an Aurora Postgres DB. I can connect to it through psql. I can also connect to it through Datagrip. I'm able to run queries.
But using the exact same connection settings through nodejs I get a connection timeout. The connection settings are exactly the same as the ones I'm using in psql and Datagrip.
pool = new Pool({
user: "myuser",
host: "localhost",
database: 'somedb',
password: "password",
port: 1234
});
pool.query('SELECT column1, column2 FROM test ORDER BY id ASC', (error, results) => {
if (error) {
response.status(400).send(`Error: ${error.message}`);
return;
}
response.status(200).json(results.rows);
})
The same code works on the server directly. It just no longer works when trying to connect via ssh tunnel. (although it used to work)
The problem was the version of the pg npm package. I updated to a newer package and the connection started working again.

error: role "root" does not exist when accessing a Postgresql Server hosted on AWS RDS from local Nodejs

I created an Elastic Beanstalk with a Nodejs app on it. Then from the environments' configurations I created a postgres RDS.
I can connect to the postgres server from a console:
psql --host=aa1682linxrnijl.c09xht5iw0ld.us-east-1.rds.amazonaws.com --port=5432 --username=monolithDB --password --dbname=postgres
And I also have an existing table in it.
result of \dt
But when I try to connect from my Nodejs localhost, using node-postgres:
const { Client } = require("pg");
const client = new Client({
host: 'aa1682linxrnijl.c09xht5iw0ld.us-east-1.rds.amazonaws.com',
password: 'XXX',
port: 5432,
database: 'postgres',
user: 'monolithDB'
});
client.connect()
I get an error:
error: role "root" does not exist
It seems to me that the node-postgres doesn't recognize the 'monolithDB' as a user. How can I properly connect to the Postgres server?

Node OracleDB connection not working in remote server

I'm using oracledb with Node and ExpressJS to develop my app. In localhost, I have no problems in my connections, but I need to configure the project in a server that connects to the database that it's in other server. When I do that, I get the error (Translated from spanish):
Error: Error: ORA-12154: TNS: the specified connection identifier could not be resolved.
This is my configuration code:
const oracleDbConfig = {
user: "myUser",
password: "myPassword",
connectString: "192.168.6.129:1521/myDatabase",
}
Also, if I install my project in the database server and I run the same configuration using localhost, it works with no problem:
const oracleDbConfig = {
user: "myUser",
password: "myPassword",
connectString: "localhost:1521/myDatabase",
}
Please, help me to understand the error.
Troubleshooting:
NetworkCheck: Is your dbnode reachable? : dig,ping,host,nslookup
FirewallCheck: Can you connect to the dbnode oracle listener? nc -vz dbnode 1521
ServiceCheck: Is the database service myDatabase exposed in listener on dbnode? dbnode>$lsnrctl status | grep myDatabase
If you can mark these tree points a success, you should be able to connect.
Best of luck.
I solved the problem changing the connection string as the documentation says:
const oracleDbConfig = {
user: "myUser",
password: "myPassword",
connectString: "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.6.129)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=myDatabase)))"
}
Both ways are valid, but I only have made it work with this.

Right way to connect to Google Cloud SQL from Node.JS

I followed the example on how to set up Node.JS to work with Cloud SQL, and generally got it to work, but with some workarounds on how to connect to the SQL server. I am unable to connect in the proper way passing the INSTANCE_CONNECTION_NAME to the socketPath option of the options variable for the createConnection() method. Instead, as a temporary workaround, I currently specify the server's IP address and put my VM IP address into the server's firewall settings to let it through.
This all works, but I'm now trying put it together properly before publishing to AppEngine.
How can I get it to work?
The following code works fine:
function getConnection ()
{
const options =
{
host: "111.11.11.11", //IP address of my Cloud SQL Server
user: 'root',
password: 'somePassword',
database: 'DatabaseName'
};
return mysql.createConnection(options);
}
But the following code, which I am combining from the Tutorial and from the Github page, which is referred to in the Tutorial, is giving errors:
function getConnection ()
{
const options =
{
user: 'root',
password: 'somePassword',
database: 'DatabaseName',
socketPath: '/cloudsql/project-name-123456:europe-west1:sql-instance-name'
};
return mysql.createConnection(options);
}
Here's the error that I'm getting:
{ [Error: connect ENOENT /cloudsql/project-name-123456:europe-west1:sql-instance-name]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'connect',
address: 'cloudsql/project-name-123456:europe-west1:sql-instance-name',
fatal: true }
What am I doing wrong? I am concerned that if I publish the app to AppEngine with the IP address, I won't be able to allow the incoming traffic into the SQL server?
I met similar error while testing 'coud sql'.
error message : Error: connect ENOENT /cloudsql/xxx-proj:us-central1:xxx-instance
solution :
+----------------------------------------------------------+ wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O
cloud_sql_proxy chmod +x cloud_sql_proxy sudo mkdir /cloudsql;
sudo chmod 777 /cloudsql ./cloud_sql_proxy -dir=/cloudsql &
=> now node js server can connect to mysql
refer to guide : https://cloud.google.com/appengine/docs/flexible/nodejs/using-cloud-sql
Are you deploying your AppEngine app to the same region as the SQL database? (europe-west1)
The documentation at https://cloud.google.com/sql/docs/mysql/connect-app-engine states "Your application must be in the same region as your Cloud SQL instance."

Resources