I'm using Heroku's Postgres add-on in one of my NodeJS apps. The code seems to work but any connections to the database using this module hangs and never responds with neither a success or a failure.
To investigate the issue, I tried connecting to the database through my terminal using pgcli <db url> but got the following error:
could not send SSL negotiation packet: Resource temporarily unavailable.
Further details:
Heroku's console shows that the database is healthy, up, and running.
Last time I used the database (around 3 months ago) it was working perfectly.
Any newly created databases from the same add-on work as expecting.
Any help would be appreciated.
Go to heroku settings, find your environmental variables.
Find the DATABASE_URL variable, copy its value
Go to your project add .env file and add the environmental variable there
Install and follow instructions on env module .e.g https://www.npmjs.com/package/dotenv
Add your connection config
.env
DATABASE_URL=verylongstring
index.js
require('dotenv').config()
const pg = require('pg'
const pgPool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
})
If your using github or some other service to store your code make sure to not send the .env file with you since its only for local development. Heroku has the environmental variables in your app.
.gitignore
.env
Related
I'm trying to deploy this Fullstack app to a DigitalOcean droplet:
https://github.com/maismin/stock-portfolio-app-demo
I installed MongoDB like how to documentation said and then I started it and didn't do anything else (I don't know if I need to actually make a database or not this is the first time I'm using MongoDB). Then I cloned the repo, then I installed NPM with npm install, then I just copied this in to the terminal:
PORT=3000
MONGODB_URI='LINK TO YOUR MONGDB SERVER'
MONGODB_URI_DEV='LINK TO YOUR LOCAL MONGODB SERVER'
MONGODB_URI_TEST='LINK TO LOCAL MONGODB SERVER'
IEX_URI=https://cloud.iexapis.com/stable
IEX_KEY='YOUR IEX KEY'
JWT_SECRET='YOUR SECRET'
and replace this MONGODB_URI='LINK TO YOUR MONGDB SERVER' with
MONGODB_URI='xxx.xxx.xxxx' (my servers IP, this is where I really have no idea what to do and am just trying things). I also did the MONGODB_URI_DEV and MONGODB_URI_TEST with the same. The IEX and JWT variables are fine and I knew what to do there.
I got this error when I npm run dev:
See https://webpack.js.org/plugins/environment-plugin for example.
Error connection to MongoDB: The 'uri' parameter to 'openUri()' must be a string, got "undefined". Make sure the first parameter to 'mongoose.connect()' or 'mongoose.createConnection()' is a string.
[nodemon] app crashed - waiting for file changes before starting...
This error is due to invalid mongodb URL, ensure that it is correct and the .env or the file where the URL is stored is imported correctly..
it might also be the issue with the .env, so add the enviroment variables directly through digital Ocean: https://thecloudhub.com/tag/digitalocean-droplet-environment-variables/
Did you add the .env file?
PORT=3000
MONGODB_URI='LINK TO YOUR MONGDB SERVER'
MONGODB_URI_DEV='LINK TO YOUR LOCAL MONGODB SERVER'
MONGODB_URI_TEST='LINK TO LOCAL MONGODB SERVER'
IEX_URI=https://cloud.iexapis.com/stable
IEX_KEY='YOUR IEX KEY'
JWT_SECRET='YOUR SECRET'
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 try to connect my database to express, but I've this message.. on PostgreSQL
throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string'
if somebody had fix the same problem ;)
when you installed Postgresql did you set the password for Postgres user? Because I had this same issue and I solved it by doing this silly but apparently important step when you installed Postgres.
run the shell and then use the command \password.
Then should put this same password in the code you use to connect the database with express.
I had this same error message come up when I tried to connect my website running locally to my database running locally.
I was able to connect my local website to my live database. And my live website worked properly with my live database too.
But I couldn't connect my local website (or Postman) to my local database.
However, I just received help from a friend to fix this. I added this line to the top of my pool.js file:
import dotenv from 'dotenv/config';
The problem was that it wasn't accessing the environment variables from the pool.js file. Although, my friend said a better solution would be to add the 'dotenv/config' to a script in the package.json file, but he wasn't sure what the proper syntax was for that, so I'm going to try to find that out next.
It could be password value undefined/null. If you put the password inside .env file, make sure to config it. In my program when the password value was undefined, it logged this error.
The syntax is
nodemon -r dotenv/config src/server.js
I am trying to deploy my first app to heroku using mLab MongoDB addon. How should I define the connection?
This is what I found in a NodeJS book, it does not seem to work, though. I replaced the password and database name with appropriate values.
mongoose.connect('mongodb://databasename:password#kahana.mongohq.com:10089/app26');
mongoose.connection.on('open', function() {
console.log('Mongoose connected.');
});
You should use Heroku enviroment variables. When you add a Mongo addon you should automatically have the enviroment variable available (these can also be accessed through the Heroku dashboard). So with MLAB you can get your connection string by accessing process.env.MONGOLAB_URI. So you can just do mongoose.connect(process.env.MONGOLAB_URI);
To get this to work on your localost you run heroku config > .env to export the enviroment variables to a .env file at the root of your app. Then when you run heroku local the MONGOLAB_URI env variable will be also available on your localhost.
I have working SailsJS app that I want to deploy to Openshift, but as usual it doesn't go smoothly.
Here's what I did so far:
rhc app create myApp nodejs-0.10
rhc cartridge add mongodb-2.4
After these two, I can see that app is created and when I visit given URL, I got Welcome page.
I installed RockMongo, and I see that I can visit my mongodb as well.
Since I already have code, I proceed with following:
git remote add openshift -f <openshift-git-repo-url>
git merge openshift/master -s recursive -X ours
git push openshift HEAD
After I merge my existing code with remote openshift (like in commands above), things start to go wrong.
When I visit url to application, I receive 503 Service temporarily unavailable. If I visit RockMongo and try to login with given credentials, I receive
Unable to connect MongoDB, please check your configurations.
MongoDB said:Failed to connect to: 127.10.37.130:27017: Transport endpoint is not connected.
Also, in Applications Panel, status of my application is building (and stays like that for hours). After pushing code to openshift, application stopped, and after rebuilding it (automatically) I receive some errors, where the last one is
remote: An error occurred executing 'gear postreceive' (exit code: 34)
remote: Error message: CLIENT_ERROR: Failed to execute: 'control build' for /var/lib/openshift/538f1c205004461655000227/nodejs
Does anyone has idea what's going on?
Maybe I didn't set up ports, application url, db url properly? But then again, why RockMongo stopped working?
UPDATE
Here's my mongo config:
mongo: {
module: 'sails-mongo',
user: 'admin',
password: '********',
url: process.env.OPENSHIFT_MONGODB_DB_URL + 'surge'
}
Do I need to set up server_port = process.env.OPENSHIFT_NODEJS_PORT and server_ip_address = process.env.OPENSHIFT_NODEJS_IP as well?
I have some server.js file in root of my application, and I see that these variables are used here.
Here's what I get if I run env | grep NODEJS
OPENSHIFT_NODEJS_PATH_ELEMENT=/var/lib/openshift/538f1c205004461655000227//.node_modules/.bin:/opt/rh/nodejs010/root/usr/bin
OPENSHIFT_NODEJS_PORT=8080
OPENSHIFT_NODEJS_LD_LIBRARY_PATH_ELEMENT=/opt/rh/nodejs010/root/usr/lib64
OPENSHIFT_NODEJS_IDENT=redhat:nodejs:0.10:0.0.17
OPENSHIFT_NODEJS_LOG_DIR=/var/lib/openshift/538f1c205004461655000227/app-root/logs/
OPENSHIFT_NODEJS_IP=127.10.37.129
OPENSHIFT_NODEJS_PID_DIR=/var/lib/openshift/538f1c205004461655000227/nodejs//run/
OPENSHIFT_NODEJS_VERSION=0.10
OPENSHIFT_NODEJS_DIR=/var/lib/openshift/538f1c205004461655000227/nodejs/
and here's what I get for env | grep mongo:
OPENSHIFT_ROCKMONGO_DIR=/var/lib/openshift/538f1c205004461655000227/rockmongo/
PHPRC=/var/lib/openshift/538f1c205004461655000227/rockmongo/etc/conf/php.ini
OPENSHIFT_ROCKMONGO_IDENT=redhat:rockmongo:1.1:0.0.12
OPENSHIFT_MONGODB_IDENT=redhat:mongodb:2.4:0.2.11
OPENSHIFT_MONGODB_DB_URL=mongodb://admin:PASSWORD_HERE#127.10.37.130:27017/
OPENSHIFT_MONGODB_DIR=/var/lib/openshift/538f1c205004461655000227/mongodb/
Just in case someone else stumbles upon this problem, here is what I had to do.
I created separate file config/application.js, and there I placed
module.exports = {
port: process.env.OPENSHIFT_NODEJS_PORT,
host: process.env.OPENSHIFT_NODEJS_IP,
environment: 'production'
};
Also I found what was the problem with application not starting. Post-install was failing (bower install did not finish successfully). To fix it, one should add to scripts section of package.json
"postinstall": "export HOME=/var/lib/openshift/[instance-id]/app-root/runtime/repo; ./node_modules/bower/bin/bower install"