'How to create PostgreSQL user for Node.js app?

I created a postgres user and database via this:

sudo -u postgres psql
postgres=# create database mydb;
postgres=# create user myuser with encrypted password 'mypass';
postgres=# grant all privileges on database mydb to myuser;

And I have this test script:

const { Pool, Client } = require('pg')
// pools will use environment variables
// for connection information
const pool = new Pool()
pool.query('SELECT NOW()', (err, res) => {
  console.log(err, res)
  pool.end()
})

And I run this command:

PGHOST=localhost PGUSER=myuser PGDATABASE=mydb PGPASSWORD=mypass PGPORT=5432 node test-db.js

And there's no ouput, no error or NOW().



Solution 1:[1]

sudo -u postgres createuser <username>;
sudo -u postgres createdb <dbname>;

this commands is for create user database and create database . then to ensure changes run this command \du , \du+ that shows all users in postgres

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 Moein T