502 Error: Node.js app with Postgres on Fly.io not working as expected

Hi everyone! I’m new to fly, and I’m facing an issue with my Node.js app that uses Postgres as the database and is deployed on Fly.io. I also need to mention that this is just a simple hobby app, as I am teaching myself the Express/Node.JS ecosystem. When I run the app locally, it works perfectly fine, but after deploying it to Fly.io, I’m getting a 502 error when trying to access the app through the browser.

Here are some details about my setup:

  • Node.js app using Express framework
  • Postgres database hosted on Fly.io as a separate app
  • Using the Fly.io CLI to deploy and manage my app

I’ve checked my app’s logs, and I can see that the server is running and listening on the correct port (usually 3000). I have also verified the DATABASE_URL environment variable, and it points to the correct Postgres database.

Despite all this, I’m still encountering the 502 error, and I’m not sure what’s causing it. I suspect there might be an issue with my app’s configuration on Fly.io or something related to the interaction between my Node.js app and the Postgres database.

I’ve tried some troubleshooting steps, including recreating the Fly.io app and Postgres database, but the problem persists.

Could someone please help me understand what might be causing this issue and how I can resolve it? Are there any specific configurations or settings I need to ensure when deploying a Node.js app with a Postgres database on Fly.io?

Thank you in advance for your assistance!

Hey there,

To try and debug this, there’s a few things that you can run to look and see what’s going on. Here are some docs to try below. Try and see if anything pops out at you or if there is anything funky going on. Also would you mind posting your fly.toml file (without envs or confidential info) that was generated?

  1. fly doctor · Fly Docs
  2. fly status · Fly Docs
  3. fly logs · Fly Docs

Hi there,

Thank you for your response and suggestions! I’ve gone through the documentation you provided and here are the results:

  1. fly doctor: I ran the fly doctor command, and it didn’t report any critical issues. I do notice that it states “Nope” for testing local docker instance, which you can find in the attached image.

  2. fly status: When I checked the status using fly status, it showed that my app is stopped. In my fly dashboard, it is confirmed that these apps have been suspended. This is weird because when testing this app, it has been at a running status the entire time, until I recently checked up on it again. You can find an image of it attached below.

  3. fly logs: I reviewed the logs using fly logs, and it seems that the app is starting up properly. However, I noticed some entries related to connection errors and timeouts. I’m suspecting that the issue might be related to the connection between my app and the PostgreSQL database.

To provide more context, my app is built with Node.js and Express, and I’m using the pg node module to interact with the PostgreSQL database. The database is hosted on a separate Fly.io app, and I’ve verified that the credentials and connection details are correct.

I also checked my fly.toml file. You can find it attached here.


1 Like

Thank you for providing all these images, I appreciate it.

In regards to this portion, you mentioned that you noticed some entries related to connection errors and timeouts. Do you mind posting your logs? So we can see the error messages and get a better look at what’s going on? (I should’ve said something in my first message about providing logs)

Not a problem! Here are some of the logs, but you can see the relevant warning and error. That same warning pops up multiple times. Let me know if you need anything else. Thanks!

Awesome, once again, thanks for providing logs!

It seems to be an issue with either the app or postgres being too “slow”. That connection timed out error indicates that the app is running slow. Here’s a better explanation for you here. A lot of this comes down to debugging your application/database to find out why things are running slowly. Is it a certain route, page, or are there any routes that don’t use the db that you could test, etc. You could also try to add logs to your app to see what’s working and to spot which part doesn’t load exactly.

2 Likes

Hi kaelyn,

I hope you’re doing well. I appreciate your support in troubleshooting the issues with my Fly app. My apologies for the late response.

As per your suggestions, I added detailed logs to my app and noticed that I’m encountering a connection timeout error. I suspect it might be related to either the app or the PostgreSQL database running slowly. However, I’m unable to pinpoint the exact cause.

Here’s some updated information:

  • When I tested individual routes in my app, some routes seemed slower than others, but I couldn’t find a specific pattern.
  • The logs show that the app is making the request, but it never goes through. I also tried switching from the pool module to the client module from the pg package, but the issue persists.
  • I verified the connection string in my DATABASE_URL, and it seems correct. However, when I ran a test file to connect with the same connection string, I received an error: “Error connecting to the database: Connection terminated unexpectedly.”
  • I checked the PostgreSQL database settings and configurations, and they appear to be fine.
  • I also tried flushing the DNS cache and verified the DNS name for my app in the Fly dashboard.

At this point, I’m not sure what else to try. I’m reaching out to you for any further insights or troubleshooting steps that could help me resolve this connection timeout issue.

I would appreciate any additional guidance you can provide. If there’s any specific information you need from me, please let me know, and I’ll provide it promptly.

Thank you for your continued assistance.