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!