Update -
Hurrah! It appears I neglected to consult the manual. Following the guidelines exactly for the environmental variables seems to be necessary.
Corrected code:
# PostgreSQL Database Information
PGDATABASE_TEST = user_db
PGDATABASE = user_db
PGUSER = postgres
PGPASSWORD = password
# PostgreSQL Host and Port Information
PGHOST = localhost
PGPORT = 5432
--
I am utilizing .env
variables to establish a connection with a Postgres Database.
Upon submitting data through Postman to an Express API, I encounter an error message as shown below:
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
}
Apparently, it is using my operating system username instead of the one set in the .env
file. Running node with sudo
results in the authentication error with root
.
My db.js
:
require("dotenv").config();
const { Pool } = require("pg");
// Determine which Database to use. Live environment is secure.
const isProduction = process.env.NODE_ENV === 'PRODUCTION';
const database = process.env.NODE_ENV === 'TEST' ? process.env.PG_TEST_DATABASE : process.env.PG_DATABASE;
// Construct request to Database
const connectionString = `postgresql://${process.env.PG_USER}:${process.env.PG_PASS}@${process.env.PG_HOST}:${process.env.PG_PORT}/${database}`;
// Initialize 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
# Environment - TEST for local, PRODUCTION for live.
NODE_ENV = PRODUCTION
# PostgreSQL Database Information
PG_TEST_DATABASE = user_db
PG_DATABASE = user_db
PG_USER = postgres
PG_PASS = password
# PostgreSQL Host and Port Information
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 user = await userService.createUserAccount({
user_name,
user_pass,
user_email
});
res.status(201).json({
status: "success",
user,
})
};