简体   繁体   中英

Linux - NodeJS, Express, PostgreSQL - ErrorHandler: password authentication failed for user "root"

Edit -

Success! I should have RTFM it seems. Using the environmental variables seems to be required to be EXACT to the manual.

Fixed code:

# PostgreSQL Database Infomation
PGDATABASE_TEST = user_db
PGDATABASE = user_db
PGUSER = postgres
PGPASSWORD = password

# PostgreSQL Host and Port Infomation
PGHOST = localhost
PGPORT = 5432

--

I'm using .env variables to connect to a Postgres Database.

When submitting data via Postman to a Express API I receive an error stating the following:

            throw new ErrorHandler(error.statusCode, error.message)
                  ^

ErrorHandler: password authentication failed for user "tito"
    at UserService.createUserAccount (/home/tito/Documents/GitHub/***/server/services/user.service.js:11:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async createUserAccount (/home/tito/Documents/GitHub/***/server/controllers/user.controller.js:11:18) {
  status: 'error',
  statusCode: undefined
}

As you can see, its using my OS username rather than .env set one. When running node with sudo I get the auth error with root .

My db.js :

require("dotenv").config();
const { Pool } = require("pg");

// Check which Database to use. Live is safe.
const isProduction = process.env.NODE_ENV === 'PRODUCTION';
const database = process.env.NODE_ENV === 'TEST' ? process.env.PG_TEST_DATABASE : process.env.PG_DATABASE;

// Request to Database contructed
const connectionString = `postgresql://${process.env.PG_USER}:${process.env.PG_PASS}@${process.env.PG_HOST}:${process.env.PG_PORT}/${database}`;

// Setup Pool. 
const pool = new Pool ({
    connectionString: isProduction ? process.env.DATABASE_URL : connectionString,
    ssl: isProduction ? { rejectUnauthorized: false } : false
});

console.log(`Ready at : ${connectionString}`)

module.exports = {
    query: (text, params) => pool.query(text, params),
    end: () => pool.end()
}

My .env :

# Express API Port.
PORT = 5000

# Enviroment - TEST for local. PRODUCTION for live.
NODE_ENV = PRODUCTION

# PostgreSQL Database Infomation
PG_TEST_DATABASE = user_db
PG_DATABASE = user_db
PG_USER = postgres
PG_PASS = password

# PostgreSQL Host and Port Infomation
PG_HOST = localhost
PG_PORT = 5432

My UserService :

const {
    createUserAccount_DB
} = require("../db/user.db");
const { ErrorHandler } = require("../helpers/error")

class UserService {
    createUserAccount = async (user) => {
        try {
            return await createUserAccount_DB(user);
        } catch (error) {
            throw new ErrorHandler(error.statusCode, error.message)
        }
    }
}

module.exports = new UserService();

And my createUserAccount :

const userService = require("../services/user.service");
const { ErrorHandler } = require("../helpers/error");

const createUserAccount = async (req, res) => {

    console.log("Create Account API Triggered");

    const { user_name, user_pass, user_email } = req.body;
    // const hashedPassword = hashedPassword(password);
     
    const user = await userService.createUserAccount({
        user_name,
        user_pass,
        user_email
    });

    res.status(201).json({
        status: "success",
        user,
    })
};

Success! I should have RTFM it seems. Using the environmental variables seems to be required to be EXACT to the manual.

Fixed code:

# PostgreSQL Database Infomation
PGDATABASE_TEST = user_db
PGDATABASE = user_db
PGUSER = postgres
PGPASSWORD = password

# PostgreSQL Host and Port Infomation
PGHOST = localhost
PGPORT = 5432

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM