'how to create a user in postgres to connect to nodejs?
It is my first time using postgres and I am trying to use it with node. I installed postgres in Ubuntu and created the user admin
sudo -i -u postgres
createuser --interactive
in my node code I tried to connect to postgres:
let { Client } = require('pg');
conn = new Client({host:'localhost', port:5432, database:'web-viewer', user: 'admin'});
conn.connect();
but I get this error
UnhandledPromiseRejectionWarning: error: password authentication failed for user "admin"
my pg_hba.conf was
local all postgres peer
then I changed to
local all postgres md5
to try to add a password to my admin user, but when I "createuser --interactive" again, I get this error
createuser: error: could not connect to database template1: FATAL: password authentication failed for user "postgres"
how can I prepare the postgreSQL to connect to node?
Solution 1:[1]
You didn't parse the password in your new Client
definition:
in my node code I tried to connect to postgres:
let { Client } = require('pg'); conn = new Client({host:'localhost', port:5432, database:'web-viewer', user: 'admin'}); conn.connect();
change it like the following, using the information you saved during the user creation:
let { Client } = require('pg');
conn = new Client({
host:'localhost',
port:5432,
database:'web-viewer',
user: 'admin',
password: 'yourpassword'
});
conn.connect();
btw, to avoid using sensible information (your password), consider creating environment variables.
I know the post is 8 months old, but you never know.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |