Connections to postgres database failure

Viewed 13786

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 Answers

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.

Related