MariaDB connection to ExpressJS access denied no password error - node.js

I'm trying to connect an Express app to MariaDB, both running on localhost.
I have the following code in an Express app:
const mariadb = require('mariadb');
const pool = mariadb.createPool({
user: 'testuser',
password: 'password',
database: 'test',
connectionLimit: 5
});
I can connect using 'testuser' from the MariaDB command line, but when Express executes the code above, I see the following in the console:
SQLError: (conn=63, no: 1698, SQLState: 28000) Access denied for user 'testuser'#'localhost'
...
code: 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR'
I have tried setting the testuser password to ''. Have also tried using root user credentials. I always get the same NO_PASSWORD error.
I am running this in Ubuntu 18.04 within Windows Subsystem for Linux/WSL.
Any help is appreciated.

Related

Connecting to azure flexible postgres server via node pg

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

Connecting node.JS app to Heroku PostgreSQL DB

I have been having some trouble with connecting my node.JS app to my Heroku database. I had my app working with a locally hosted Postgres database on PGadmin, but when I tried moving towards deploying on Heroku with a Heroku Postgres DB I started getting a variety of errors.
This was my base code that worked locally and that I tried to switch over to Heroku's DB (some variables hidden with ***):
const Client = require('pg');
const client = new Client.Client({
host: "***.amazonaws.com",
user: "***",
password: "***",
database: "***",
port: 5432,
ssl: true,
sslmode: require,
});
The "ssl" and "sslmode" were only added when switching over.
I have triple-checked that all the values are correct. When I do it this way this is the error I get...
Error: self signed certificate
When commenting out the "ssl" part the error changes to...
Error: no pg_hba.conf entry for host '***', user '***', database '***' , SSL off
I tried researching this pg_hba.conf issue and there was suggestions I add in a line that made sure that a password was not required for all IPv4 connections but this did not change my error messages.
I am quite a bit stuck on how to solve this trouble as I can't quite find any further help online so far.

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?

Can't authenticate user with Knex.js migrate:latest in Node

The problem:
Relevant information: Windows 10, Node, knex, PostgreSQL
I'm trying to run the knex CLI command migrate:latest from cmd, knex migrate:latest --env development.
I'm getting "error: password authentication failed for user "
My first problem is with the user specified - in my knexfile.js configuration file, I am exporting the development object with a defined connection, where all of my environment variables are defined.
development: {
client: 'pg',
connection: {
host: "localhost",
port: process.env.DB_PORT,
username: process.env.DB_USER,
password: process.env.DB_PW,
database: process.env.DB_NAME
}
}
So although I'm trying to log into the DB as 'postgres', knex is trying to log me in as the Windows user from the associated error message, which is my only account on my personal computer. I am logged in as that user, PostgreSQL was installed with this user account, and the database was initialized with this user.
Using psql, I can log into the database specified in my knex config using the password specified in my knex config. In fact, I'm using the same password for all users to reduce the number of variables in my problem.
In addition to trying to solve the problem this way, I have all of my authentication settings in my pg_hba.conf file set to 'trust' - but that isn't fixing the problem either.
Try to hardcode username and password and DB name to knexfile instead of using process.env.XXXX and try again.
Sounds like your environment variables are not passing to the node correctly.

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.

Resources