0

I'm trying to set up a react app that uses graphql with a postgresql database. Right now im only trying to set up a pg adaptor to my existing postgresql db that I host locally. When running node pgAdaptor.js i get a rejected promise saying that the db does not exist, but the name it says doesnt exist is the username, and not the correct db name, which is 'filter'.

I want to know what could possibly cause node to try and use a username as a database name??

I am using the dotenv node package to read in the db properties into process.env, and i then use these to configure the server correctly, yet it still tries to connect to the same username. Even when i change the config in the .env file.

pgAdaptor.js:

require ('dotenv').config ();
const pgPromise = require ('pg-promise');

const pgp = pgPromise ({}); // Empty object means no additional config required

const config = {
  host: process.env.POSTGRES_HOST,
  port: process.env.POSTGRES_PORT,
  database: process.env.POSTGRES_DB,
  user: process.env.POSTGRES_USER,
  password: process.env.POSTGRES_PASSWORD,
};

const db = pgp (config);

exports.db = db;

db.one ('select * from users').then (res => {
  console.log (res);
});

db.env:

POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=filter
POSTGRES_USER=morbidmerve
POSTGRES_PASSWORD=

error:

(node:2449) UnhandledPromiseRejectionWarning: error: database "morbidmerve" does not exist
    at Connection.parseE (/Users/morbidmerve/dev/test/node_modules/pg/lib/connection.js:601:11)
    at Connection.parseMessage (/Users/morbidmerve/dev/test/node_modules/pg/lib/connection.js:398:19)
    at Socket.<anonymous> (/Users/morbidmerve/dev/test/node_modules/pg/lib/connection.js:120:22)
    at Socket.emit (events.js:197:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:145:17)
(node:2449) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:2449) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

when i test the pgAdaptor.js functionality i simply run

node pgAdaptor.js

I can confirm that the database filter exists and contains at least one record on each table.

This is supposed to work according to the tutorial found at https://blog.harveydelaney.com/setting-up-graphql-express-and-postgresql/ .

Any help is greatly appreciated!

  • Re-check what's really in your `config` object, because this doesn't add up. – vitaly-t Apr 05 '19 at 05:17
  • bingo! thanks for the suggestion....the config object contains a bunch of undefined values. You suppose that if the config object contains the right values then it should work fine? – Merveille van Eck Apr 05 '19 at 07:09
  • Yes. And when config contains undefined values, those are replaced from the defaults - `pgp.pg.defaults`. That's what you are probably getting now. – vitaly-t Apr 05 '19 at 07:11

1 Answers1

0

Thanks to vitaly-t's comment on my question I found the problem:

The dotenv package was failing to read in the configs into process.env because the file wasnt named '.env' explicitly but rather only had a .env extension. i.e. mine was db.env which isn't a valid config file name apparently. Hence, after changing the file name to literally .env the dotenv package read in the configs perfectly.

Also stack overflow is awesome!