I am having issues with setting up the sequelize module for node.js.
First I set up a username and password in postgresql:
postgres=# CREATE USER testuser WITH PASSWORD 'test';
Here is my initialize code:
var Sequelize = require('sequelize');
var sequelize = new Sequelize('testdb', 'testuser', 'test', {
host: 'localhost',
port: 5342,
dialect: 'postgres'
});
sequelize.query('select * from mytbl').success(function(tbl){
console.log('success');
});
However, when i run this short bit of code I get the following error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: Failed to authenticate for PostgresSQL. Please double check your settings.
I am not sure what I am doing wrong? If anyone can help it would be greatly appreciated. Thanks!
This example worked when I changed the port to 5432.
Related
I am trying to implement an auth service using node-express-postgres.
I had the pool configed as such:
const Pool = require('pg').Pool;
const pool = new Pool({
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
port: 5432
});
module.exports = pool;
I am trying to do the following call as a simple test for connection:
const express = require('express');
const router = express.Router();
const pool = require('../db');
const bcrypt = require('bcryptjs');
router.post('/login', async (req, res) => {
try {
let temp = await pool.query("SELECT * FROM records");
console.log(temp)
} catch (error) {
console.log(error.message);
}
});
When I send a post request to this endpoint my app crash with the following error:
Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string
I have checked all my env vars and they are correct.
Any idea why it is failing to do any operation on the postgres DB?
It seems, that node didn't read .env file.
You can check it with
console.log(process.env.DB_PASSWORD);
It can be fixed with package 'dotenv', for example.
npm i --save dotenv
And then in first line in index.js
require('dotenv').config();
For me configuring 'dotenv' resolved error
require('dotenv').config();
Check your postgres password if it's correct. I had a similar problem while working on a mac, by default the
posgreSQL user is "posgres" and password is "root"
In my case I had something like
...
USER: "postgres",
PASSWORD: "",
...
which generated the error
you probably should indicate your .env file location inside of the config
require('dotenv').config({ path: '../.env' });
dotenv configurations
Fixed it by updating npm script.
cross-env NODE_ENV=development nest start
Installed "cross-env" to set NODE_ENV.
If our code is not able to find .development.env file or unable to find password, then this error will be thrown.
Check out the path in your IDE folder, when i checked, it was by one path below, so i moved it in the correct folder and boom, it all worked.
I am new to AWS and NodeJS/Express server. The server should connect to AWS RDS Postgress Database.
I am developing using Visual Studio Code on MacOS
I get this error when I try to run my server:
Using ts-node version 8.4.1, typescript version 3.6.3
(node:2377) UnhandledPromiseRejectionWarning: SequelizeConnectionError: password authentication failed for user “userNB”
at connection.connect.err (…node_modules/sequelize/lib/dialects/postgres/connection-manager.js:154:24)
at Connection.connectingErrorHandler (…..node_modules/pg/lib/client.js:191:14)
at Connection.emit (events.js:198:13)
at Connection.EventEmitter.emit (domain.js:448:20)
I am saving my username, pass,...etc as environment variables in /.bash_profile
my Sequelize code:
import {Sequelize} from 'sequelize-typescript';
import { config } from './config/config';
onst c = config.dev;
export const sequelize = new Sequelize({
"username": c.username,
"password": c.password,
"database": c.database,
"host": c.host,
dialect: 'postgres',
storage: ':memory:',
});
Is this kind of error related to my NodeJS code or AWS settings?
How can I fix it ?
I'm using sequelize like this and it's working for me:
DATABASE_URI=postgresql://username:password#amazonhost:5432/database_name
export const sequelize = new Sequelize(DATABASE_URI);
Also make sure you've the correct password and the IP from which you're trying to connect is whitelisted for database. You can test this using postgres client cli.
Let me know if it works for you.
Thanks!
you are missing sequelize params to create a new instance. create a new sequelize instance like this:
const sequelize = new Sequelize('database', 'username', 'password', {
host: your_database_host,
dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
});
for more information, see sequelize docss
https://sequelize.org/master/manual/getting-started.html
I was trying to connect to a remote server mongoDB through SSH and made the configurations as provided
import tunnel from 'tunnel-ssh';
const config = {
username: 'username',
Password: 'password',
host: process.env.SSH_SERVER, //192.168.9.104
port: 22,
dstHost: 'localhost',
dstPort: process.env.DESTINATION_PORT, //27017
localHost: '127.0.0.1',
localPort: 27018
};
This is the config that has been defined where i need to connect to the remote server 192.168.9.104. So the particular is chosen as the SSH host. Username and password for the same is provided. and the connection made is as follows.
class DB {
initDB() {
tunnel(config, (error, server) => {
if (error) {
console.log('SSH connection error: ' + error);
}
const url = 'mongodb://127.0.0.1:27018/myDBname';
mongoose.connect(url, { useNewUrlParser: true });
mongoose.plugin(toJson);
mongoose.plugin(setProperties);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log('DB connection successful');
});
});
}
}
But when the db.init() function is called following error pops up
events.js:183
throw er; // Unhandled 'error' event
^
Error: All configured authentication methods failed
I am not able to figure out where the config goes wrong. i have tried using 127.0.0.1 for dstHost. as well as put the 192.168.9.104 as the dstHost as well but the error persists. kevin lee suggests a similar approach. this question is used as an example
There was an error with the documentation which suggested the config as mentioned above with the key "Password" but it should be "password" so the config would look something like this
const config = {
username: 'username',
password: 'password',
host: process.env.SSH_SERVER, //192.168.9.104
port: 22,
dstHost: 'localhost',
dstPort: process.env.DESTINATION_PORT, //27017
localHost: '127.0.0.1',
localPort: 27018
};
Rest of the implementation is spot on and tested.
I am running NodeJS microservice that talks to Postgres database. But when I am trying to start the service I am getting below error. I do not have any idea why this error is popping up.
Error:
UnhandledPromiseRejectionWarning: Unhandled promise rejection(rejection id:1): error: database “root” does not exist
My DB connection details:
const pg = require(“pg”);
const client = new pg.Client({
host: “txslmxxxda6z”,
user: “mom”,
password: “mom”,
db: “mom”,
port: 5025
});
I am able to resolve this myself. The issue is in connection config. It should be database but not db so this was causing the issue. PFB answer
const client = new pg.Client({
host: “txslmoxxx6z”,
user: “mom”,
password: “mom”,
//change db to database
database: “mom”,
port: 5025
});
I'm trying to create a mysql database to node.js server. I've installed mysql module through command prompt:
npm install mysql
Then I execute the following code:
var Client = require('mysql').Client;
console.log(Client);
Console display undefined. That is, Client is undefined. Please tell me why it is undefined?
I'm following this tutorial
http://utahjs.com/2010/09/22/nodejs-and-mysql-introduction/
Maybe the the tutorial is a little bit old. Just use the instruction on the node-mysql docs:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret'
});
connection.connect();
And you should be able to connect to your MySQL database.
The node js APIs having been changing updating a lot in recent past, so it is highly possible that the tutorial you have been following is out of date according to the version you are using. You can follow the code example here I am updating or you may refer to something else, the only part that matters is it should work at minimum cost.
var mysql = require('mysql');
app.use( connection(mysql, {
host: 'myhost',
user: 'user_name',
password: 'password',
port: 3306, //port mysql
database: 'database_name',
multipleStatements: 'true' //false by default
}, 'pool'));
req.getConnection(function(err, connection) {
connection.query("SELECT * FROM `table_name`;",function (error,row){
if(!error){
//do something.....
}
else console.log("Error : "+err);
});
//do something else...
});
Thank you...!