Connections to postgres database failure

11,397

Solution 1

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();

Solution 2

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

Solution 3

For me configuring 'dotenv' resolved error

require('dotenv').config();
Share:
11,397

Related videos on Youtube

nimrod feldman
Author by

nimrod feldman

Updated on June 04, 2022

Comments

  • nimrod feldman
    nimrod feldman almost 2 years

    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?

    • Wiktor Zychla
      Wiktor Zychla about 3 years
      What gives console.log( typeof process.env.DB_PASSWORD )?
    • nimrod feldman
      nimrod feldman about 3 years
      it returns a string
    • Dhaval Darji
      Dhaval Darji about 3 years
      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
    • nimrod feldman
      nimrod feldman about 3 years
      I am seeing the same error for pool.connect()
    • Ananta K Roy
      Ananta K Roy almost 3 years
      @nimrodfeldman Were you able to solve this error?
    • siddube
      siddube almost 3 years
      Getting the same error. Anyone found a solution for this?