简体   繁体   English

Linux - NodeJS、Express、PostgreSQL - 错误处理程序:用户“root”的密码验证失败

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

Edit -编辑 -

Success!成功! I should have RTFM it seems.看来我应该有RTFM 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.我正在使用.env变量连接到 Postgres 数据库。

When submitting data via Postman to a Express API I receive an error stating the following:通过 Postman 向 Express API 提交数据时,我收到一条错误消息,说明如下:

            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.如您所见,它使用我的操作系统用户名而不是.env设置一个。 When running node with sudo I get the auth error with root .使用sudo运行节点时,出现root的身份验证错误。

My db.js :我的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 :我的.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 :我的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 :我的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.看来我应该有RTFM 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户“root”、postgresSQL 和 docker 的密码验证失败 - password authentication failed for user "root", postgresSQL and docker 使用 session 变量(Express/NodeJs)进行用户身份验证 - User authentication with session variable (Express/NodeJs) UnhandledPromiseRejectionWarning:错误:用户密码认证失败 - UnhandledPromiseRejectionWarning: error: password authentication failed for user 错误:用户“ myuser”的密码身份验证失败 - error: password authentication failed for user “myuser” 使用 JWT 进行 NodeJS 用户身份验证 - NodeJS user authentication with JWT NodeJS/mySQL - ER_ACCESS_DENIED_ERROR 用户 'root'@'localhost' 的访问被拒绝(使用密码:是) - NodeJS/mySQL - ER_ACCESS_DENIED_ERROR Access denied for user 'root'@'localhost' (using password: YES) Mongo身份验证在Node.js应用程序中失败 - Mongo Authentication Failed in Nodejs Application 当我在Node.js + Express中使用护照进行用户身份验证时会发生奇怪的事情 - Strange things happen when I use passport for user authentication in Nodejs+Express 用 postgreSQL 续集 nodejs 中的迁移错误并表达 JS - Sequelize migration error in nodejs with postgreSQL and express JS 使用NodeJS,Express-Session和Passport的身份验证失败 - Authentication with NodeJS, Express-Session and Passport fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM