SCRAM-SERVER-FIRST-MESSAGE: client password must be a string

Viewed 21270

Ive read documentation from several pages on SO of this issue, but i havent been able to fix my issue with this particular error.

    throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string')
    ^

Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string
    at Object.continueSession (C:\Users\CNFis\Desktop\WulfDevelopments\ThePantry\node_modules\pg\lib\sasl.js:24:11)
    at Client._handleAuthSASLContinue (C:\Users\CNFis\Desktop\WulfDevelopments\ThePantry\node_modules\pg\lib\client.js:257:10)
    at Connection.emit (events.js:400:28)
    at C:\Users\CNFis\Desktop\WulfDevelopments\ThePantry\node_modules\pg\lib\connection.js:114:12
    at Parser.parse (C:\Users\CNFis\Desktop\WulfDevelopments\ThePantry\node_modules\pg-protocol\dist\parser.js:40:17)
    at Socket.<anonymous> (C:\Users\CNFis\Desktop\WulfDevelopments\ThePantry\node_modules\pg-protocol\dist\index.js:11:42)
    at Socket.emit (events.js:400:28)
    at addChunk (internal/streams/readable.js:290:12)
    at readableAddChunk (internal/streams/readable.js:265:9)
    at Socket.Readable.push (internal/streams/readable.js:204:10)

its as if in my connectDB() function its not recognizing the password to the database. I am trying to run a seeder.js script to seed the database with useful information for testing purposes, and if i run npm run server which is a script that just starts a nodemon server, itll connect to the DB just fine. but when i try to run my script to seed data, i am returning this error.

import { Sequelize } from "sequelize";
import colors from "colors";
import dotenv from "dotenv";

dotenv.config();

const user = "postgres";
const host = "localhost";
const database = "thePantry";
const port = "5432";

const connectDB = async () => {
  const sequelize = new Sequelize(database, user, process.env.DBPASS, {
    host,
    port,
    dialect: "postgres",
    logging: false,
  });
  try {
    await sequelize.authenticate();
    console.log("Connection has been established successfully.".bgGreen.black);
  } catch (error) {
    console.error("Unable to connect to the database:".bgRed.black, error);
  }
};

export default connectDB;

above is my connectDB() file, and again, it works when i run the server normally. but i receive this error only when trying to seed the database. Ill post my seeder script below:

import dotenv from "dotenv";
import colors from "colors";
import users from "./data/users.js";

import User from "./models/userModel.js";

import connectDB from "./config/db.js";

dotenv.config();
console.log(process.env.DBPASS);

connectDB();

const importData = async () => {
  try {
    await User.drop();
    await User.sync();

    await User.bulkCreate(users);

    console.log("Data Imported".green.inverse);
    process.exit();
  } catch (e) {
    console.error(`${e}`.red.inverse);
    process.exit(1);
  }
};

const destroyData = async () => {
  try {
    await User.bulkDestroy();

    console.log("Data Destroyed".red.inverse);
    process.exit();
  } catch (e) {
    console.error(`${e}`.red.inverse);
    process.exit(1);
  }
};

if (process.argv[2] === "-d") {
  destroyData();
} else {
  importData();
}

4 Answers

Add your .env file in your project, I think your .env file is missing in your project folder. add like this: enter image description here

So, i may have figured this out by playing around in another project with sequelize, as it turns out, the initial connection to the database in my server.js file, honestly means nothing. Unlike Mongoose where the connection is available across the whole app. its not the same for Sequelize this connection that it creates is only apparent in certain places, for example i was trying the same process in my other project as i am here, except i was trying to read data from my DB using the model that i built with sequelize and i was receiving the same type error, i went into where i defined the model and made a sequelize connection there, and i was then able to read from the database using that object model.

Long story short, to fix the error in this app i have to place a connection to the database in the seeder.js file or i have to place a connection in the User model (this is ideal since ill be using the model in various places) to be able to seed information or read information from the database.

today i have same problem like this, so if you use database with type relational. you must define password from database.

const user = "postgres";
    const host = "localhost";
    const database = "thePantry";
    const password = "yourdatabasepassword"; if null > const password = ""; 
    const port = "5432";

but, if you use database with type non-relational, as long as the attributes are the same, you can immediately run the program as you defined it

I also faced this issue and another solution different from the accepted solution here solved my issue, so I wanted to explain that to this lovely community, too.

Firstly, when I faced the issue, ran my project in debug mode and reached the code below.

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

The problem here is actually obvious when I saw first, there is a problem in .env file as mentioned in the solutions above. In my process.env is defined as like as following line: DATABASE_URL=postgres://username:password@IP_adress:port/db_name and my config.js file is in the following format:

module.exports = {
  "development": {
    "url":"postgres://username:password@IP_adress:port/db_name",
    "dialect": "postgres",    
  }, ...
}

So as a solution, I come with the following fix for the parameters that are inside Sequelize(...). My solution below is totally worked for me and I hope it also works for you too.

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.url, config);
}

Finally, the point you need to be careful about what you have written to the config file. That's the most important in this case.

Farewell y'all.

Related