JS postgres - wrong username connection - node.js

So im trying to connect my local postgres DB to my node.js server.
But im facing the ennoying issue of wrong username.
error: password authentication failed for user "Yoav Genish"
error log:
name: 'error',
length: 169,
severity: 'FATAL',
code: '28P01',
The Yoav Genish is my win10 user name and postgres just default it as the superadmin.
I have a user with premission for the DB called me and i specified it in a .env file.
Here's the .env file:
HOST='localhost'
USER = 'me'
DATABASE= 'api'
PASSWORD= 'password'
PORT=5432 '
and here's the code:
require('dotenv').config();
const Pool= require('pg').Pool;
const ENVDATA = {
HOST: process.env.HOST,
USER: process.env.USER,
DATABSE: process.env.DATABASE,
PORT: process.env.PORT,
PASSWORD: process.env.PASSWORD
}
var pool = new Pool(ENVDATA);
Also i verified the the ENVDATA is indeed the data in the .env file.
again, the issue is that i want to log into the local DB with the me username, but i keep getting the above error.
thanks in advance!

It appears to be a syntax problem. One should use Lower Case properties when passing data to the Pool meaning
const ENVDATA = {
host : process.env.HOST,
user: process.env.USER,
.
.
.
But i found an even "clener" solution and it's using the URI String instead:
const PGURI = `postgres://${process.env.USER}:${process.env.PASSWORD}#$
{process.env.HOST}:${process.env.PORT}/${process.env.DATABASE}`;
var pool = new Pool({
connectionString: PGURI
});

Related

Nodejs connect to Redshift using temporal credentials error

I'd like to connect to Redshift using temporal credentials.
I'd tried connecting with master username and password and it works fine. The problem of the temporal credentials is the username that is following format:
username: 'IAM:awsuser'.
So I think the connection is not understanding correctly the ":". So it always through invalid password. I have try this username and password from the Redshift query-editor and it connects without any problem.
This is the configuration I'm using:
const configRed = {
host: 'redshift-cluster-name.aaaaaaa.eu-west-1.redshift.amazonaws.com',
user: 'IAM:awsuser',
password: data.DbPassword,
database: 'dev',
port: 5439,
idleTimeoutMillis: 0,
max: 10000
};
redshiftPool = new psql.Pool(configRed);
redshiftCon = await redshiftPool.connect();
I have also tried using the username with encodeURIComponent:
user: encodeURIComponent('IAM:awsuser'),
It through next error:
"errorMessage": "password authentication failed for user \"IAM:awsuser\"",
Could be possible to change the connection URL in the PG library, for some custom URL like:
jdbc:redshift:iam://examplecluster.abc123xyz789.us-west-2.redshift.amazonaws.com:5439/dev
Specifying "ssl: true" in the params argument when creating the Pool object indeed works:
const configRed = {
host: 'redshift-cluster-name.aaaaaaa.eu-west-1.redshift.amazonaws.com',
user: 'IAM:awsuser',
password: data.DbPassword,
database: 'dev',
port: 5439,
idleTimeoutMillis: 0,
max: 10000,
ssl: true
};

How to check that mysql connection is valid

I'm using node, with 'mysql2' module, how can I check that the config settings are correct and connecting to the database correctly?
These are my connection settings, however I want to check that the database connection is valid before any queries are done, such as allowing the user to login.
const mysql = require('mysql2');
const conn = mysql.createPool({
host: 'hostname',
port: port,
database: 'db',
user: 'root',
password: 'password',
dateStrings: 'date'
});
const pool = conn.promise();
module.exports = pool;
I added this code (before the module.exports line) to throw an error, but is there a better way of doing this? The error code -3008 relates to the 'ENOTFOUND' code, which is what I receive when the host name was incorrect and the connection failed.
pool.execute('SELECT * FROM table LIMIT 1')
.catch(err => {
if (err.errno === -3008) {
throw new Error('Can\'t connect to database please check connection settings.');
}
});
are you able to connect via command line in your mysql?
mysql -h localhost -u myname -ppassword mydb
Also, to test the connection before execute the query, I suggest you to use .getConnection

Can't connect to SQL Server from my VS Code extension (node.js)

I'm developing VS Code extension and want to get some data from SQL Server database. I've tried many different examples but non of them are working for me.
const Connection = require('tedious').Connection;
const config = {
user: 'ssrs', // tried userName, username
password: 'ssrs',
server: 'localhost',
options: {
database: 'imdb'
}
}
const connection = new Connection(config);
connection.on('connect', function(err: string) {
if (err) {
console.log(err);
} else {
console.log('Connected');
}
});
OR
await sql.connect('mssql://ssrs:ssrs#localhost/imdb')
const result = await sql.query`select 1 as one`
console.dir(result)
OR
const sql = require("mssql");
const conn = new sql.ConnectionPool({
database: "imdb",
server: "localhost",
driver: "msnodesqlv8",
userName: "ssrs",
password: "ssrs"
});
conn.connect().then(() => {
console.log('Connected');
});
ConnectionError: Login failed for user ''.
Connecting via sqlcmd:
The port is also open (1433) as I can telnet to it.
Messages from SQL Server log:
Date 2019-05-08 11:19:02 AM Log SQL Server (Current - 2019-05-05
2:53:00 PM)
Source Logon
Message Login failed for user ''. Reason: An attempt to login using
SQL authentication failed. Server is configured for Integrated
authentication only. [CLIENT: 127.0.0.1]
Date 2019-05-08 11:19:02 AM Log SQL Server (Current - 2019-05-05
2:53:00 PM)
Source Logon
Message Error: 18456, Severity: 14, State: 58.
Trying to connect with Powershell:
PS C:\WINDOWS\system32> Invoke-Sqlcmd -query "select CURRENT_USER, USER_NAME()" -ServerInstance "localhost" -username "ssrs" -password "ssrs" -Database 'imdb'
Column1 Column2
------- -------
ssrs ssrs
As shown in your screenshot, you're able to connect via SQLCMD utility. This means there's no issue with the user or the authentication mode in SQL Server.
The error message is however misleading and other users already experienced this here.
I'm no expert of node.js but I'd recommend you to better understand how the mssql plugin and Connection object work. I think that some missing setting in your configuration is making the connection attempt fail.
For example, this code you posted is wrong (userName is not valid):
const sql = require("mssql");
const conn = new sql.ConnectionPool({
database: "imdb",
server: "localhost",
driver: "msnodesqlv8",
userName: "ssrs",
password: "ssrs"
});
conn.connect().then(() => {
console.log('Connected');
});
It should be:
const sql = require("mssql");
const conn = new sql.ConnectionPool({
database: "imdb",
server: "localhost",
driver: "msnodesqlv8",
user: "ssrs",
password: "ssrs"
});
conn.connect().then(() => {
console.log('Connected');
});
The reason you can't connect is written in the log:
Reason: An attempt to login using SQL authentication failed. Server is
configured for Integrated authentication only.
You have to change the authentication mode (which you already did according to the screenshot posted) and restart the SQL Server service with Agent. Did you restart the service after changing the mode?

PostgreSQL error when trying to connect from node application

FATAL: no PostgreSQL user name specified in startup packet
Logs from my postgreSQL instance in my kubernetes cluster when trying to connect to it by doing the following:
const { POSTGRES_DATABASE, POSTGRES_USERNAME, POSTGRES_PASSWORD } = require('../config/config');
const Sequelize = require('sequelize');
const conn = new Sequelize(POSTGRES_DATABASE, {
username: POSTGRES_USERNAME,
password: POSTGRES_PASSWORD
});
config/config.js
const config = {
POSTGRES_DATABASE: 'postgres://postgres/postgresdb',
POSTGRES_USERNAME: 'postgresadmin',
POSTGRES_PASSWORD: 'admin123',
SERVER_PORT: '5000'
}
module.exports = config;
I am using SequelizeJS in nodeJS.
http://docs.sequelizejs.com/
It seems like the requests are connecting alright due to seeing the attempts in the postgreSQL logs. However something goes wrong and I wonder if it's wrong with permissions in postgres or the node service.
Appreciate some help or ideas
According to the Sequelize documentation that you linked, the args to creating a new connection are db, username, password. Your code did db, and then an object with username and password keys. This object is not correct and caused it to not find the username or password. Try this instead:
const { POSTGRES_DATABASE, POSTGRES_USERNAME, POSTGRES_PASSWORD } = require('../config/config');
const Sequelize = require('sequelize');
const conn = new Sequelize(POSTGRES_DATABASE, POSTGRES_USERNAME, POSTGRES_PASSWORD);
Sequelize does allow for an optional options object as the last argument, but the username and password are not expected to be in there.
This page of the documentation feels confusing to me.
http://docs.sequelizejs.com/manual/installation/usage.html
I believe what it says is that you either need to define everything in the object, or do the database, username, and password as separate arguments, but I don’t see it supporting database in the arguments and username / password in the object.
It seems like this should work as well:
const conn = new Sequelize({
database: POSTGRES_DATABASE,
username: POSTGRES_USERNAME,
password: POSTGRES_PASSWORD
});

Is it possible to "CREATE DATABASE ..." with pg-promise and PostgreSQL (9.5)?

Question is:
1) Can pg-promise be used to create a new database (schemas, et. al.)?
Using node package 'pg-promise' I can't seem to figure out if it's possible to create a new database. I can connect to an existing database and have no issue there. However, when I run without a database name I get an error:
This is what I am trying:
host = host || '127.0.0.1'
port = port || '5432'
const pgp = require('pg-promise')(pgpInitOptions)
// database connection details
const pgConnectionOptions = {
host: host,
port: port,
user: user,
password: pass
}
// database instance
const localDB = pgp(pgConnectionOptions)
let escapedDbName = dbName.replace(/\"/g, '""');
let sql = 'CREATE DATABASE "' + escapedDbName + '"';
localDB
.any(sql)
.then((results) => {
console.log('creation of database successful', results)
})
.catch((error) => {
console.error('creation of database failed', error)
})
Then, I get this error:
creation of database failed
Error: database "jeff" does not exist
"jeff" is my ubuntu login name.
First, you need to connect to an existing database, using an admin account which has the right to create new databases.
Then you can create new databases via a regular query...
const pgp = require('pg-promise')(/* initialization options */);
const db = pgp({
database: 'any-existing-db',
port: 5432,
user: 'postgres', // any admin user
password: 'admin-password'
});
await db.none('CREATE DATABASE $1:name', ['my_database']);
I am running such code locally, and it works fine.

Resources