When running locally and connecting to Heroku with the same DATABASE_URL, I'm able to connect and query the db perfectly fine.
When the same code is running on Heroku -- the query never returns and will eventually time out.
DB Code:
const connectionString = process.env.DATABASE_URL;
const pool = new Pool(
{
connectionString: connectionString,
ssl: true
}
);
public async query(text: any, params: any): Promise<any> {
return pool.query(text, params);
}
This works as expected when I run on localhost and attach to the Heroku Postgres add-on (Hobby tier), but when deployed to Heroku it no longer works. The DATABASE_URL is loaded correctly though.
Weird one, but if anyone runs into this problem here was our solution:
We used the pg package and were running version ^7.12.1, which allowed us to connect and query the Heroku hosted database locally, but not when the server was deployed on Heroku.
The fix was updating the pg package, which we are now running version ^8.5.1.
Related
I'm trying to put my REST API built in Node and with PostgresSQL to Heroku. So, I created my application to Heroku, and created his own database. At this point, I tryied to commit, and the build worked corretly. This until I tryied to make some calls to the API. If the api calls I do has the uncorrect method, or doesn't exists, it gives me the correct error, but when the call is correct, there is a 503 error, with code H12, and description that is timeout error. Here is the code of one of the calls to the database that I'm testing:
router.get('/allpoints', async (req,res) =>{
try {
const points = await pool.query(
`SELECT nome, latitudine,longitudine
FROM luogo`);
res.json(points.rows);
}catch(err){
console.error(err.message);
}
});
Here there are the information about how I connect to the database.
const pool = new Pool({
connectionString: process.env.DATABASE_URL || 'postgresql://postgres:psw#localhost:5432/campione',
ssl: process.env.DATABASE_URL ? true : false
})
module.exports = pool;
The build on Heroku seems to work properly.
I read this question: Heroku h12 Timeout Error with PG / Node.js
It says that you have to put res.end() where there is not res.json(), but here there is the res.json(). So, I thought that the issue could be that there is an error that the route manage, and can't give back anything. So, I changed from console.log(err) to res.json(err), and the API response with `ssl self signed, as an error. At this point, in the second file, I put ssl as false by default, but it gaves me error because there is no SSL. I searched for a really long time for a solution, but I have not been able yet to fix the issue.
Someone thinks he knows what should I change? Thank you in advice
this option in databse config maybe useful
ssl: {
rejectUnauthorized : false,
}
I’m trying to perform a simple query on a local database. I expect this query to return the schema names of the database.
I am running Postgres version 13.1 and I installed it by following the steps shown here: https://postgresapp.com/
As per guidelines on Postgres Wiki, I'm including config file changes, I only manually edited settings to enable logging.
This computer is running MacOS Big Sur Version 11.0.1.
I'm using Node.js and Postgres is running on port 5432 and I can access it with psql.
The relevant changes I've made are the following:
Endpoint in server.js:
router.post('/mock_call', async (ctx) => {
try {
console.log('sup')
await sql.mockCall()
ctx.body = {
status: "It's good",
data: 'good'
}
} catch (e) {
console.log(e)
ctx.body = {
status: "Failed",
data: e
}
ctx.res.statusCode = 422;
}
})
SQL File:
require("openssl")
const { Pool, Client } = require('pg');
const client = new Client({
user: 'user1',
host: 'localhost',
database: 'postgres',
password: 'mypass',
port: 5432,
});
module.exports = {
mockCall: function () {
console.log('mockCall begin')
client.connect(err => {
if (err) {
console.error('connection error', err.stack)
} else {
console.log('connected')
}
})
console.log('before query')
client.query("SELECT schema_name FROM information_schema.schemata", (err, res) => {
if (err) {
console.log('theres an error:')
console.log(err)
};
console.log('theres a response:')
console.log(res)
for (let row of res.rows) {
console.log(JSON.stringify(row));
}
client.end();
});
}
}
Logs that actually get printed out when I hit the endpoint on localhost:
sup
mockCall begin
before query
Postgresql Logs (not helpful it's as if the server never gets hit):
This exact project and code is working on my personal local computer and the query goes through as expected. It used to be working on a Heroku server I had set up. The only difference with the Heroku server is that the connection is made like so:
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
This connection had been working on a server I had for over a year. My database was running out of space so I upgraded from a hobby database to a standard plan on Heroku, the app continued to work. A couple weeks after this upgrade I pushed a new commit which included a couple new features on the app and this broke the postgresql connection. After this push I immediately checked out my last commit which was working and pushed that one, the issue however was still there.
I currently have the program running on my personal local computer but I need to move it back to Heroku as quickly as possible. The pictures and logs I've included above are the result of running my app locally on my friends computer, which seems to be having the same issue I'm having on Heroku so I'm hoping if I figure out the issue on his local computer I'll be able to solve what's going on in Heroku.
These are the logs that are printed out from my personal local computer which is working:
Edits:
Running psql -d postgres -U user1 -h localhost -p 5432 successfully connects me to the database on the command line.
The new features I added was a new endpoint for my apps customers. This commit works fine on my personal local computer, so I don't think it's an issue with the new features that I added. Additionally, since then I've reverted to my previous commit which used to be working so none of that new code is present anymore.
I'm running the entire app locally on my friends computer. I set up Postgres from scratch just as I did a year ago on my computer. However now, only my personal local computer is working.
I haven't changed anything on pg_hba.conf on either setup. This is what they both look like:
At first I thought the problem would be with Heroku since my local app was working fine. However after reaching out and talking for a couple days with support they said:
Hi there,
It looks like your application is able to successfully connect to the database, but something else in the application or framework is preventing the data from being retrieved. Unfortunately, as this is an application issue it falls outside the nature of the Heroku Support policy. I recommend searching our Knowledge Base or asking the community on Stack Overflow for more answers.
Turns out I was using an old version of pg, 7.8. I upgraded to 8.5 and now it works.
I developed a node app server that get data requested through a postgres (sequelize ORM) which send the data to my react nextjs app when requested.
A few days ago I tried to host the application on heroku, which is when all hell broke loose. the application stopped working. I was getting a socket hangup error. I followed the error stack trace until i think i pinpointed the error on when my server request data from my database. I realized there are no more data coming through. Therefore, I am left hanging with a pending promise. I rebuild my backend. I also went back to previous commit when it was working and the application is still not working. The front-end is working bc it is running on a different host but my backend is not receiving data from my db anymore. I have tried everything i can think of and read a bunch of article on stack overflow and Github but have not figure it out yet. Also, I can no longer seed my seed file for some reason (also return pending promise).
ANY HELP WILL BE REALLY APPRECIATED.
I figure out what was the problem. My configuration on the server side with my database was not working. Therefore, I was getting an unresolved promise which caused the application to just keep loading while waiting for the data. By changing my config file to this:
`
const Sequelize = require("sequelize");
let database = process.env.DATABASE_URL;
let sequelize = "";
process.env.DATABASE_URL
? (sequelize = new Sequelize(database))
: (sequelize = new Sequelize(database, "postgres", "", {
dialect: "postgres",
logging: false,
}));
`module.exports = sequelize;
and adding my postgres database manually on my terminal when running heroku pg:psql to see and create my data base. I was able to make the application work once deployed on Heroku.
I get the following error message when trying to connect my NodeJS Express app to my MongoDB server hosted on Heroku. It works fine on my local dev env.
MongoNetworkError: failed to connect to server [cluster0-shard-00-00-hl87n.mongodb.net:27017] on first connect [MongoNetworkError: connection 4 to cluster0-shard-00-00-hl87n.mongodb.net:27017 closed]
I am getting the connect string the following way.
const server = process.env.MONGODB_URI;
const database = process.env.MONGODB_DB;
const user = process.env.MONGODB_USER;
const password = process.env.MONGODB_PASSWORD;
mongoose.connect(`mongodb+srv://${user}:${password}#${server}/${database}`, { useNewUrlParser: true });
The enviroment variables are active on my Heroku instance, I confirmed this by doing heroku config, and also with a console log that correctly prints out all of the above information.
Any idea what could be pointing to this issue, Im out of trails to follow.
Mongodb Cloud requires that you Whitelist the IP that the connections are coming from. Unfourtunatly Heroku doesn't supply you with an IP address so you need to allow access from all IP addresses if you want it to work.
Incase anyone else falls upon this issue that was the fix.
I have a problem deploying a Nodejs app with a Postgresql database. The database comes from Heroku itself (Heroku Postgres add-on, hobby-dev). My app refused to connect to the database.
I found where the problem came from but I can't find a clean solution. And I think I could have misunderstood something (I'm new to Node and Heroku).
Heroku automatically gives me an environment variable DATABASE_CONFIG that includes the port:
postgres://username:password#hostname:port/databasename
Then, to connect with pg in my app, I use process.env.DATABASE_CONFIG as a connection string. I do something like:
const client = new Client({
connectionString: connectionString,
})
client.connect()
This fails to connect.
But if instead of using this environment variable, I cheat and change it, removing the port number from this connection string, it works.
I don't know why but the problem is that Heroku gives you this DATABASE_URL with the port included and you can't change it.
Did I do something wrong? Is there a clean solution to avoid that?
(because what I did is ugly as I hard-coded the DATABASE_CONFIG without the port directly in my code)
Thanks for your help!
First off, I would avoid using new Client() as this can lead to your connection being bottlenecked. Instead, use connection pooling. You can read a more indepth answer into why you want to do that here.
As for you direct issue, personally I have had trouble connecting to heroku postgres databases in the past, but here is a (basic) typical setup that works for me 99% of the time:
let pg = require('pg');
if (process.env.DATABASE_URL) {
pg.defaults.ssl = true;
}
// include an OR statement if you switch between a local dev db and
// a remote heroku environment
let connString = process.env.DATABASE_URL || 'postgresql://postgres:password#localhost:localpostgresport/yourlocaldbname';
const { Pool } = require('pg');
const pool = new Pool({
connectionString : connString
});
My first guess would be that it may have to do with ssl not being enabled. I know that has cause me problems in the past. Secondly, you want to make sure that you uses the process.env.DATABASE_URL, as environment variable should be set as the postgres connection string by Heroku.