Yoav Genish
Yoav Genish

Reputation: 97

JS postgres - wrong username connection

So im trying to connect my local postgres DB to my node.js server. But im facing the ennoying issue of wrong username.

error: password authentication failed for user "Yoav Genish"

error log:

  name: 'error',
  length: 169,
  severity: 'FATAL',
  code: '28P01',

The Yoav Genish is my win10 user name and postgres just default it as the superadmin.

I have a user with premission for the DB called me and i specified it in a .env file.

Here's the .env file:

HOST='localhost'
  USER = 'me'
  DATABASE= 'api'
  PASSWORD= 'password'
  PORT=5432 '

and here's the code:

require('dotenv').config();
const Pool= require('pg').Pool;
const ENVDATA = {
    HOST: process.env.HOST,
    USER: process.env.USER,
    DATABSE: process.env.DATABASE,
    PORT: process.env.PORT,
    PASSWORD: process.env.PASSWORD
    }
var pool = new Pool(ENVDATA);

Also i verified the the ENVDATA is indeed the data in the .env file.

again, the issue is that i want to log into the local DB with the me username, but i keep getting the above error.

thanks in advance!

Upvotes: 0

Views: 899

Answers (1)

Yoav Genish
Yoav Genish

Reputation: 97

It appears to be a syntax problem. One should use Lower Case properties when passing data to the Pool meaning

const ENVDATA = {
    host : process.env.HOST,
    user: process.env.USER,
    .
    .
    .

But i found an even "clener" solution and it's using the URI String instead:

const PGURI = `postgres://${process.env.USER}:${process.env.PASSWORD}@$
{process.env.HOST}:${process.env.PORT}/${process.env.DATABASE}`;
var pool = new Pool({
    connectionString: PGURI
});

Upvotes: 2

Related Questions