how to create a user in postgres to connect to nodejs? - javascript

It is my first time using postgres and I am trying to use it with node. I installed postgres in Ubuntu and created the user admin
sudo -i -u postgres
createuser --interactive
in my node code I tried to connect to postgres:
let { Client } = require('pg');
conn = new Client({host:'localhost', port:5432, database:'web-viewer', user: 'admin'});
conn.connect();
but I get this error
UnhandledPromiseRejectionWarning: error: password authentication failed for user "admin"
my pg_hba.conf was
local all postgres peer
then I changed to
local all postgres md5
to try to add a password to my admin user, but when I "createuser --interactive" again, I get this error
createuser: error: could not connect to database template1: FATAL: password authentication failed for user "postgres"
how can I prepare the postgreSQL to connect to node?

You didn't parse the password in your new Client definition:
in my node code I tried to connect to postgres:
let { Client } = require('pg');
conn = new Client({host:'localhost', port:5432, database:'web-viewer', user: 'admin'});
conn.connect();
change it like the following, using the information you saved during the user creation:
let { Client } = require('pg');
conn = new Client({
host:'localhost',
port:5432,
database:'web-viewer',
user: 'admin',
password: 'yourpassword'
});
conn.connect();
Follow the doc.
btw, to avoid using sensible information (your password), consider creating environment variables.
I know the post is 8 months old, but you never know.

Related

Why connecting to remote server using Sequelize uses local machie IP as target DB server IP?

I'm mounting an API in Google Cloud Run that connects to an MySQL DB server using Sequelize.
This is the standard setup:
index.js file
const DB_CONFIG = require('../db/db.config');
const Sequelize = require('sequelize')
let sequelize = new Sequelize(
DB_CONFIG.NAME,
DB_CONFIG.USER,
DB_CONFIG.PASSWORD,
{ host: DB_CONFIG.HOST, dialect: DB_CONFIG.DIALECT, pool: DB_CONFIG.POOL }
)
db.config.js file
module.exports = {
NAME: process.env.DB_NAME,
HOST: process.env.DB_HOST,
USER: process.env.DB_USER,
PASSWORD: process.env.DB_PASSWORD,
DIALECT: process.env.DB_DIALECT,
POOL: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
};
The problem comes when I start the project, it fails with an error that shows it is trying to connect to the same IP as the local machine public IP:
{
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlState: '28000',
sqlMessage: "Access denied for user 'DB_USER'#'LOCAL_MACHINE_IP' (using password: YES)",
sql: undefined
}
The problem happens either if I write the final values directly in the db.config file or the values are read from process.env.
It is important to not that I'm using the Google Cloud Secret Manager to inject this values to process.env.
Also, the execution logs in Google Cloud Run shows that the API is trying to connect using the local IP.
What could be happening, and how can I search the cause of this error?
There is nothing wrong with that IP address as the host name in a mysql user account indicates the client you are connecting from, not the mysql server's IP address you are connecting to.
See mysql manual on account names for details.

Why am I getting a timeout error when exporting a mariadb connection pool in Node.js?

EDIT
I found the error. The mistake was very obvious: I did not include the
require("dotenv").config(); in the connection.js file. Without this, the database connection simply fails after a timeout because it does not have any connection details.
I found an update log from the Mariadb Node.js connector team stating they have a few errors where Mariadb does not provide sufficient error messages (it sometimes only offers a "timeout" without further information), so I changed what I was looking for, and found the mistake.
For anyone getting a similar error message, this can mean anything, so check all parts of your code!
Original Post
I am trying to get familiar with Nodejs and express, but ran into an issue that I can't seem to solve:
When creating a Mariadb database pool in a seperate file, and exporting the pool using module.exports, I am having trouble using the same pool in another file. I get a timeout error when trying to use the pool to query a database.
If I use the exact same code in the same file instead of two separate files, the query works perfectly, so I think there is something going wrong during module.exports = pool.
Am I missing something? Thanks in advance!
I have two files:
index.js:
// import express web framework
const express = require("express");
//create an express application
const app = express();
const pool = require('./database/connection')
const cors = require('cors');
//middleware
app.use(cors())
app.use(express.json())
getData = async () => {
data = await pool.query("call stored_procedure")
console.log (data)
}
getData()
app.listen(3001, () => {
console.log('Serving running on port 3001')
})
and connection.js:
//import mariadb library
const mariadb = require("mariadb")
//function that create mariadb connection pool for database
const createPool = () => {
try {
return (
mariadb.createPool({
connectionLimit: 10,
host: process.env.MARIADB_HOST,
user: process.env.MARIADB_USER,
password: process.env.MARIADB_PASSWORD,
database: process.env.MARIADB_DB,
port: 3306
})
)
}
catch (err) {
console.error('Failed to connect to database: ')
console.error(err)
}
}
const pool = createPool()
//export database connection pool
module.exports = pool
Running this app results in the following error (after some time):
path_to_dir/node_modules/mariadb/lib/misc/errors.js:57
return new SqlError(msg, sql, fatal, info, sqlState, errno, additionalStack, addHeader);
^
SqlError: (conn=-1, no: 45028, SQLState: HY000) retrieve connection from pool timeout after 10001ms
(pool connections: active=0 idle=0 limit=10)
at Object.module.exports.createError (path_to_dir/node_modules/mariadb/lib/misc/errors.js:57:10)
at Pool._requestTimeoutHandler (path_to_dir/node_modules/mariadb/lib/pool.js:345:26)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) {
text: 'retrieve connection from pool timeout after 10001ms\n' +
' (pool connections: active=0 idle=0 limit=10)',
sql: null,
fatal: false,
errno: 45028,
sqlState: 'HY000',
code: 'ER_GET_CONNECTION_TIMEOUT'
}
I found the error. The mistake was very obvious: I did not include the require("dotenv").config(); in the connection.js file. Without this, the database connection simply fails after a timeout because it does not have any connection details. I found an update log from the Mariadb Node.js connector team stating they have a few errors where Mariadb does not provide sufficient error messages (it sometimes only offers a "timeout" without further information), so I changed what I was looking for, and found the mistake.
For anyone getting a similar error message, this can mean anything, so check all parts of your code!

ER_ACCESS_DENIED_ERROR: Access denied on nodeJS

MySQL is connecting to a different unknown IP when trying to connect.
Code Below:
.env
MYSQL_HOST=domain.example.com
MYSQL_USER=****
MYSQL_PASSWORD=****
MYSQL_DB=****
MYSQL_PORT=3306
connection.js
const mysql = require("mysql");
exports.pool = mysql.createPool({
connectionLimit: 10,
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DB,
});
controller/cron
pool.getConnection((err, connection) => {
if (err) {
console.log("ERROR: ", err);
} else { ... insert data .... }
});
With this i'm getting error:
try connecting to remote mysql...
Error: Error: ER_ACCESS_DENIED_ERROR: Access denied for user '****'#'112.200.198.66' (using password: YES)
Tested in MySQL Workbench and successfully connected. I'm just curious where the IP came from when the public IP of that domain is different. Also tried using the IP instead to connect and still giving me the same error.
If you're using MySQL 8 the issue could be that the Node mysql driver uses a different authentication plugin - see Node.js can't authenticate to MySQL 8.0

Unable to make request to my mariadb database using node server

I'm currently learning how to setup a node server and I'm making an API that performs some requests on my MariaDB database hosted on my VPS.
The problem is that when I make a POST request which makes a SQL request to the database, the connection times out and the server shuts down.
I have tried to add new users to MariaDB with all privileges, I tried use sequelize too.
But none of those solutions work, it still times out every time I make a query to my database.
I can connect to phpmyadmin and make some request on it, so I think that my database is running fine.
Here is my code:
router.post('/login', async function(req,res) {
let conn;
try {
// establish a connection to MariaDB
conn = await pool.getConnection();
// create a new query
var query = "select * from people";
// execute the query and set the result to a new variable
var rows = await conn.query(query);
// return the results
res.send(rows);
} catch (err) {
throw err;
} finally {
if (conn) return conn.release();
}
})
The way I connect to my database in my database.js file
const pool = mariadb.createPool({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABSE_NAME,
});
// Connect and check for errors
module.exports={
getConnection: function(){
return new Promise(function(resolve,reject){
pool.getConnection().then(function(connection){
resolve(connection);
}).catch(function(error){
reject(error);
});
});
}
}
module.exports = pool;
And my error:
Node.js v17.0.1
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Server started
/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/misc/errors.js:61
return new SqlError(msg, sql, fatal, info, sqlState, errno, additionalStack, addHeader);
^
SqlError: retrieve connection from pool timeout after 10001ms
at Object.module.exports.createError (/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/misc/errors.js:61:10)
at timeoutTask (/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/pool-base.js:319:16)
at Timeout.rejectAndResetTimeout [as _onTimeout] (/Users/alexlbr/WebstormProjects/AlloEirb/server/node_modules/mariadb/lib/pool-base.js:342:5)
at listOnTimeout (node:internal/timers:559:11)
at processTimers (node:internal/timers:500:7) {
text: 'retrieve connection from pool timeout after 10001ms',```
Three possibilities come to mind:
There is a typo in database name:
database: process.env.DATABSE_NAME
database: process.env.DATABASE_NAME
Your environment variables are not being properly set. Are you using dotenv to load these from an .env file?
https://www.npmjs.com/package/dotenv
If not, how are you setting the process.env values at runtime?
If the environment values are indeed set:
verify that these environment values are correct
verify which interface your MariaDB server is listening on:
It's possible the server is using a bind-address configuration and only listening on 127.0.0.1 (which is the default on Debian/Ubuntu)
You want to make sure the server is listening on: 0.0.0.0 (all interfaces, not only localhost)

Initialize a mysql connection in adonis v4

I want to initialize a MySQL connection with credentials that i'm getting from a master database, is possible to do this on AdonisV4?
using adonis lucid
I can't set the credentials in config, because they're specific for every request that is made.
You would invoke npm library related to mysql in terminal shell.
npm i --save mysql
AdonisJs uses the connection value defined inside the config/database.js file. Thus, you may to create a configuration file for a single database connection in your JavaScript application like following:
You can create an initial environment file for your master database like :
HOST=127.0.0.1
PORT=3306
NODE_ENV=development
CACHE_VIEWS=false
For several connections, you would setup your config file like this:
'use strict'
const Env = use('Env')
const Helpers = use('Helpers')
module.exports = {
connection: Env.get('DB_CONNECTION', 'mysql1'),
mysql1: {
client: 'mysql',
connection: {
host: Env.get('DB_HOST', 'database_host_1'),
port: Env.get('DB_PORT', '3306'),
user: Env.get('DB_USER', 'my_database_user_1'),
password: Env.get('DB_PASSWORD', 'OhMyAdonis'),
database: Env.get('DB_DATABASE', 'adonis')
}
},
mysql2: {
client: 'mysql',
connection: {
host: Env.get('DB_HOST', 'database_host_2'),
port: Env.get('DB_PORT', '3306'),
user: Env.get('DB_USER', 'my_database_user_2'),
password: Env.get('DB_PASSWORD', 'OhMySecondAdonis'),
database: Env.get('DB_DATABASE', 'another_adonis')
}
}
}
So, you can select any of the connections defined by the database configuration file at runtime with:
Database
.connection('mysql1')
.table('users')
Database
.connection('mysql2')
.table('users')
However, if yo have a lot of databases, this approach impacts the RAM server. So, if you have a master database like your case, choose connection dynamically following an ORM for Node.JS using a SQL query builder like Objection.js with knex - based support for MySQL.
A multitenancy approach could be adopted from here.

Categories

Resources