The problem:
Relevant information: Windows 10, Node, knex, PostgreSQL
I'm trying to run the knex CLI command migrate:latest from cmd, knex migrate:latest --env development.
I'm getting "error: password authentication failed for user "
My first problem is with the user specified - in my knexfile.js configuration file, I am exporting the development object with a defined connection, where all of my environment variables are defined.
development: {
client: 'pg',
connection: {
host: "localhost",
port: process.env.DB_PORT,
username: process.env.DB_USER,
password: process.env.DB_PW,
database: process.env.DB_NAME
}
}
So although I'm trying to log into the DB as 'postgres', knex is trying to log me in as the Windows user from the associated error message, which is my only account on my personal computer. I am logged in as that user, PostgreSQL was installed with this user account, and the database was initialized with this user.
Using psql, I can log into the database specified in my knex config using the password specified in my knex config. In fact, I'm using the same password for all users to reduce the number of variables in my problem.
In addition to trying to solve the problem this way, I have all of my authentication settings in my pg_hba.conf file set to 'trust' - but that isn't fixing the problem either.
Try to hardcode username and password and DB name to knexfile instead of using process.env.XXXX and try again.
Sounds like your environment variables are not passing to the node correctly.
Related
I deployed my server on Heroku but when I make any requests it returns a "500 Internal Server" error. It runs fine locally though. Could anyone help figure out what's going on?
When I check my logs this is what I'm getting.
2021-06-08T18:43:09.715406+00:00 app[web.1]: error: no pg_hba.conf entry for host "3.90.138.215", user "detmvsbueicsez", database "da9nlve42hcp91", SSL off
Repo Link: https://github.com/zcason/Restaurant-Review-Server
Live App: https://restaurant-review-phi.vercel.app/
As mentioned here on Heroku help, this indicate that there was a failed authentication attempt to the database, so the connection couldn't be established. This can happen because of different reasons.
In your case i suspect it's something related to not using ssl.
So after taking a look on the code provided in the github repo i noticed you are using knex and getting the connection string from .env
Try this :
Just add this ?ssl=true and append it to the end of DATABASE_URL in your .env file.
Edit your server.js (i didn't take a good look at the code so you need to add this ssl: { rejectUnauthorized: false } in your connection config) :
const db = knex({
client: 'pg',
connection: {
connectionString: DATABASE_URL,
ssl: { rejectUnauthorized: false }
}
});
Also make sure you're using the wright user and password and database name etc
OR Alternatively :
Run this command heroku config:set PGSSLMODE=no-verify in terminal to omit the ssl configuration object and set PGSSLMODE to no-verify
I have been having some trouble with connecting my node.JS app to my Heroku database. I had my app working with a locally hosted Postgres database on PGadmin, but when I tried moving towards deploying on Heroku with a Heroku Postgres DB I started getting a variety of errors.
This was my base code that worked locally and that I tried to switch over to Heroku's DB (some variables hidden with ***):
const Client = require('pg');
const client = new Client.Client({
host: "***.amazonaws.com",
user: "***",
password: "***",
database: "***",
port: 5432,
ssl: true,
sslmode: require,
});
The "ssl" and "sslmode" were only added when switching over.
I have triple-checked that all the values are correct. When I do it this way this is the error I get...
Error: self signed certificate
When commenting out the "ssl" part the error changes to...
Error: no pg_hba.conf entry for host '***', user '***', database '***' , SSL off
I tried researching this pg_hba.conf issue and there was suggestions I add in a line that made sure that a password was not required for all IPv4 connections but this did not change my error messages.
I am quite a bit stuck on how to solve this trouble as I can't quite find any further help online so far.
I was trying to deploy my Node JS application to Heroku. Heroku was connected to my Github account and deployed through Github. In my Node JS application, I created a SQL pool file awsPool.js using the following code:
const mysql = require('mysql');
const awsPool = mysql.createPool({
connectionLimit: 10,
host: "myDb.abcdefg.eu-west-2.rds.amazonaws.com",
user: "myUsername",
password: "myPassword",
port: 3306,
database: 'myDb',
debug: false
});
module.exports = awsPool;
And imported it in my Express application. The pool contains credentials such as my username and password, so I set them as ignored in .gitignore. However, when trying to deploy the application to Heroku, Heroku gave me this error:
Error: Cannot find module './awsPool'
I understand this is likely due to awsPool.js being not tracked in my Github, but how can I properly hide my credentials and deploy to Heroku?
You can use ponto .env files that will define your credentials. So when you are going to deploy your application on Heroku you will only need to define your environment variables
I am trying to run my first migration which creates a single table in a Heroku postgres database.
When I try to run knex migrate:latest --env development I receive the error
Error: Unable to acquire a connection
Things I've tried:
adding ?ssl=true to the end of my connection string stored in process.env.LISTINGS_DB_URL as I'm aware this is sometimes a requirement to connect with heroku
setting the env variable PGSSLMODE=require
I also stumbled across this article where someone has commented that knex will not accept keys based on environment. However, I'm attempting to follow along with this tutorial which indicates that it does. I've also seen numerous other references which re-enforce that.
I'll also add that I've been able to connect to the database from my application and from external clients. I'm only encountering this error when trying to run the knex migration.
Furthermore, I've tried identifying how I can check what is being sent as the connection string. While looking at the knex documentation:
How do I debug FAQ
If you pass {debug: true} as one of the options in your initialize
settings, you can see all of the query calls being made.
Can someone help guide me in how I actually do this? Or have I already successfully done that in my knexfile.js?
Relevant files:
// knex.js:
var environment = process.env.NODE_ENV || 'development';
var config = require('../knexfile.js')[environment];
module.exports = require('knex')(config);
// knexfile.js:
module.exports = {
development: {
client: 'pg',
connection: process.env.LISTINGS_DB_URL,
migrations: {
directory: __dirname + '/db/migrations'
},
seeds: {
directory: __dirname + '/db/seeds'
},
debug: true
},
staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},
production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}
};
As noted by #hhoburg in comments below, the error Error: Unable to acquire a connectionis a generic message indicating something is incorrect with Knex client configuration. See here.
In my case, Knex wasn't referencing process.env.LISTINGS_DB_URL in knexfile.js because:
that variable was set in my .env file
the dotenv module wasn't be referenced/called by Knex
The correct way of setting this up is detailed in the knex issue tracker here.
Step 1:
First install dotenv:
npm i dotenv --save
Create a .env file in the root of your project, add:
DATABASE_URL=postgres://...
Step 2:
In the beginning of your knexfile.js, add:
require('dotenv').config();
Change the postgres connection to something like:
{
client: 'postgresql',
connection: process.env.DATABASE_URL,
pool: {
min: 0,
max: 15
},
migrations: {
directory: ...
},
seeds: {
directory: ...
}
}
I'm not sure if this will help at all, but I began running into the same issue today on my local environment. After way too much searching, I found that this is the new error message for an invalid connection configuration or a missing connection pool. After fiddling around with it for way too long, I switched my connection to use my .env file for the configuration environment; I had been using a hard-coded string ('dev') in my knex.js file, which didn't work for some reason.
Is your .env file working properly? Did you try messing with the pool settings, and are you positive your username and password are correct for the staging and production database?
I hope that link helps!
if you are getting this error in nodejs try removing this line
myDb.destroy().then();
I received this same error in the same situation. Turns out I forgot to provision a database before migrating, so there was nothing to connect to.
To fix this error,
Before running:
heroku run knex migrate:latest
I ran this command:
heroku addons:create heroku-postgresql
and that worked nicely.
I got this error when trying to update data to a database before running corresponding migration.
I'm following this tutorial here.
https://github.com/mozilla/openbadges-badgekit/wiki/BadgeKit-Self-Hosting-Guide#badgekit-api-configuration
It says, when you run the API use this command source env_local
The problem is, I'm new to node and not really sure how I run the API. I've downloaded all the stuff, installed node, and got a simple hello world program working with node. I just don't know how I actually run the API. I thought I had to run the procfile, but when I do node procfile I get an error saying cannot find module badegkit\badgekit-api\start
As a hacky ass solution, I figured this out. I tried setting PATH in environment variables to env_local, as well as doing SET path = env_local in the command window and neither worked. However, if you browse to the badgekit api folder then app then lib, there's a db.js file in there.
Here's what I did, their stuff is commented out.
var options = {
driver: 'mysql',
// host: process.env.DB_HOST,
// user: process.env.DB_USER,
// password: process.env.DB_PASSWORD,
// database: process.env.DB_NAME,
host: "127.0.0.1",
user: "username",
password: "password",
database: "dbname",
}
Just put in your mysql creds and then you can run the db migrate. Obviously this isn't ideal as anywhere else that uses env is still going to be screwy, but it got me to step 2.
2014-09-29 Edit
For all those curious, as of right now, node isn't really supported on windows per se. I ran into so many issues with python and gyp, that I ended up just spinning up a linux box in hyper v and hosting it there.
The idea behind that command is to load the environment variables setup in env_local. By deafult, they are set up as a few export commands, which should put the key=value pairs into your running environment.
I actually removed the export strings and stored them as .env_dev, so a file with:
DB_HOST=localhost
DB_NAME=badgekitapi
...etc...
And the command I used to run things was: nf start -e .env_dev
With the API running, and able to store badges, you can now follow the tutorials to run openbadges-badgekit - so you can actually make and issue some badges ; )
Good luck!