3

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?

6
  • What gives console.log( typeof process.env.DB_PASSWORD )? Commented Apr 20, 2021 at 10:47
  • 2
    it returns a string Commented Apr 20, 2021 at 10:55
  • I suggest to implement connect method for your db in your db.js file, so, when it's included in another file, i'll automatically run connect operation and then do other stuff Commented Apr 20, 2021 at 11:13
  • I am seeing the same error for pool.connect() Commented Apr 20, 2021 at 11:18
  • @nimrodfeldman Were you able to solve this error? Commented Jul 25, 2021 at 9:09

6 Answers 6

8

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();
Sign up to request clarification or add additional context in comments.

Comments

3

For me configuring 'dotenv' resolved error

require('dotenv').config();

Comments

1

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

Comments

0

you probably should indicate your .env file location inside of the config

require('dotenv').config({ path: '../.env' });

dotenv configurations

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.