502 Bad Gateway – Rails App Cannot Connect to PostgreSQL

kamkara-civ
FREE

2 months ago

Hello Railway Support,

My Rails 8 app, deployed in the Singapore region, is returning a 502 Bad Gateway. Below are the relevant details:

Production Logs (showing repeated restarts and PostgreSQL recovery):

Stopping Container
Mounting volume on: …/vol_2pxznr0my2ga1ott
Starting Container
Certificate will not expire

PostgreSQL Database directory appears to contain a database; Skipping initialization

2025-05-31 12:17:16.440 UTC [6] LOG:  starting PostgreSQL 16.8 
2025-05-31 12:17:16.440 UTC [6] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2025-05-31 12:17:16.448 UTC [6] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2025-05-31 12:17:16.462 UTC [29] LOG:  database system was interrupted; last known up at 2025-05-31 12:00:24 UTC
2025-05-31 12:17:16.573 UTC [29] LOG:  database system was not properly shut down; automatic recovery in progress
2025-05-31 12:17:16.580 UTC [29] LOG:  redo starts at 0/276EFE8
2025-05-31 12:17:16.580 UTC [29] LOG:  invalid record length at 0/276F020: expected at least 24, got 0
2025-05-31 12:17:16.580 UTC [29] LOG:  redo done at 0/276EFE8
2025-05-31 12:17:16.593 UTC [27] LOG:  checkpoint starting: end-of-recovery immediate wait
2025-05-31 12:17:16.620 UTC [27] LOG:  checkpoint complete
2025-05-31 12:17:16.632 UTC [6] LOG:  database system is ready to accept connections


Production Configuration (database.yml excerpt):

production:
  primary: &primary_production
    <<: *default
    url:      <%= ENV["LNCLASSAPP_DBURL"] %>
    database: <%= ENV["LNCLASSAPP_DBNAME"] %>
    username: <%= ENV["LNCLASSAPP_DBUSERNAME"] %>
    password: <%= ENV["LNCLASSAPP_DBPASSWORD"] %>
    host:     <%= ENV["LNCLASSAPP_DBHOST"] %>
    port:     <%= ENV["LNCLASSAPP_DBPORT"] %>

Environment Variables:

LNCLASSAPP_DBURL:      postgresql://postgres:VakzAteZLypWhTYalriQHvirZIdOnyLb@postgres.railway.internal:5432/railway
LNCLASSAPP_DBNAME:     railway
LNCLASSAPP_DBHOST:     postgres.railway.internal
LNCLASSAPP_DBPORT:     5432
LNCLASSAPP_DBUSERNAME: postgres
LNCLASSAPP_DBPASSWORD: VakzAteZLypWhTYalriQHvirZIdOnyLb

Issue Details:

  • PostgreSQL logs show the database entering recovery and then becoming ready.

  • Rails/Puma starts but immediately restarts containers, leading to 502.

  • The app never stays running long enough to serve requests.

Request:

  1. Can you confirm if the database’s recovery is taking unusually long or if there’s an underlying issue?

  2. Is there a recommended delay or configuration change so that Rails waits until PostgreSQL is fully ready?

  3. Should I adjust any specific Railway settings for Singapore deployments to prevent this 502 loop?

Our production environment is blocked. Any guidance is greatly appreciated.

Thank you,
Kamate Vakaromoko
Lnclass Education

Solved$10 Bounty

20 Replies

kamkara-civ
FREE

2 months ago

My Rails application deployed on Railway is encountering a 502 error because it cannot connect to the PostgreSQL database (Singapore region).

Could you let me know how to ensure Rails waits until PostgreSQL is fully ready before attempting to connect, or indicate any necessary adjustments to restore the connection?


2 months ago

Hello,

Try disabling serverless on postgres if it doesnt fix the issue then try using the DATABASE_PUBLIC_URL


smolpaw
HOBBYTop 5% Contributor

2 months ago

Let's try to fix your db first then we can go on to fix your rails app.

Why is the db entering recovery mode over and over again ? do you have any idea and was it always like this ?
Have you enabled serverless mode for postgres ?
Is it a railway db deployment or custom image with your own configuration ?


smolpaw

Let's try to fix your db first then we can go on to fix your rails app.Why is the db entering recovery mode over and over again ? do you have any idea and was it always like this ?Have you enabled serverless mode for postgres ?Is it a railway db deployment or custom image with your own configuration ?

kamkara-civ
FREE

2 months ago

Hi @smolpaw, thank you for your response.

I’ve enabled serverless mode to avoid using more credit while things are working correctly.


kamkara-civ

Hi @smolpaw, thank you for your response.I’ve enabled serverless mode to avoid using more credit while things are working correctly.

smolpaw
HOBBYTop 5% Contributor

2 months ago

It is not advised to enable serverless mode for postgres or any kind of db that relies on disk operation.
If you read the docs you will see that the serverless mode works purely by looking at the network activity.

Inactivity is based on the detection of any outbound packets
https://docs.railway.com/reference/app-sleeping

When your rails app goes to sleep it disconnects from postgres where it was maintaining an active network connection, after 10 minutes your postgres goes to sleep too.
If the db was performing disk operations railway wouldn't know about it and kill it anyway leading to data corruption and loss.

So turn off serverless mode for postgres. you can keep the rails app in serverless mode.


smolpaw

Let's try to fix your db first then we can go on to fix your rails app.Why is the db entering recovery mode over and over again ? do you have any idea and was it always like this ?Have you enabled serverless mode for postgres ?Is it a railway db deployment or custom image with your own configuration ?

kamkara-civ
FREE

2 months ago

Okay, thanks for these recommendations; I’ll try them right away and get back to you in a moment.


smolpaw

Let's try to fix your db first then we can go on to fix your rails app.Why is the db entering recovery mode over and over again ? do you have any idea and was it always like this ?Have you enabled serverless mode for postgres ?Is it a railway db deployment or custom image with your own configuration ?

kamkara-civ
FREE

2 months ago

Hello,

I’m sharing my custom database configuration, which has been working correctly for the past six months. However, I may have made an error, so I’m providing it again for review.

I’ve noticed the following warning on the DATABASE_PUBLIC_URL field:

This variable references a public endpoint through this variable:
DATABASE_PUBLIC_URL → RAILWAY_TCP_PROXY_DOMAIN
Connecting to a public endpoint will incur egress fees. That might happen if this variable, DATABASE_PUBLIC_URL, is used to establish a connection to a database or another service.
You can avoid the egress fees by switching to a private endpoint (e.g., RAILWAY_PRIVATE_DOMAIN). Check out our documentation for more information!

Production configuration (config/database.yml):

production:
  primary: &primary_production
    <<: *default
    url:      <%= ENV["DB_URL"] %>
    database: <%= ENV["DB_NAME"] %>
    username: <%= ENV["DB_USERNAME"] %>
    password: <%= ENV["DB_PASSWORD"] %>
    host:     <%= ENV["DB_HOST"] %>
    port:     <%= ENV["DB_PORT"] %>

Credentials contents (config/credentials.yml.enc, production section):

########################### DATABASE CONFIG ###########################
production:
  DB_URL:      postgresql://postgres:vvlvqzMzhvGBWgHkrAwQQsvDqtpdMFKS@postgres.railway.internal:5432/railway
  DB_NAME:     railway
  DB_HOST:     postgres.railway.internal
  DB_PORT:     5432
  DB_USERNAME: postgres
  DB_PASSWORD: vvlvqzMzhvGBWgHkrAwQQsvDqtpdMFKS
########################### DATABASE CONFIG ###########################

I have also added all of these variables to the “Environment Variables” section in Railway to ensure they are correctly configured.

Could you please confirm whether this setup is correct or advise if any changes are needed to avoid the egress warning?

Thank you for your help.


kamkara-civ

Hello,I’m sharing my custom database configuration, which has been working correctly for the past six months. However, I may have made an error, so I’m providing it again for review.I’ve noticed the following warning on the DATABASE_PUBLIC_URL field:This variable references a public endpoint through this variable:DATABASE_PUBLIC_URL → RAILWAY_TCP_PROXY_DOMAINConnecting to a public endpoint will incur egress fees. That might happen if this variable, DATABASE_PUBLIC_URL, is used to establish a connection to a database or another service.You can avoid the egress fees by switching to a private endpoint (e.g., RAILWAY_PRIVATE_DOMAIN). Check out our documentation for more information!Production configuration (config/database.yml):production: primary: &primary_production <<: *default url: <%= ENV["DB_URL"] %> database: <%= ENV["DB_NAME"] %> username: <%= ENV["DB_USERNAME"] %> password: <%= ENV["DB_PASSWORD"] %> host: <%= ENV["DB_HOST"] %> port: <%= ENV["DB_PORT"] %>Credentials contents (config/credentials.yml.enc, production section):########################### DATABASE CONFIG ########################### production: DB_URL: postgresql://postgres:vvlvqzMzhvGBWgHkrAwQQsvDqtpdMFKS@postgres.railway.internal:5432/railway DB_NAME: railway DB_HOST: postgres.railway.internal DB_PORT: 5432 DB_USERNAME: postgres DB_PASSWORD: vvlvqzMzhvGBWgHkrAwQQsvDqtpdMFKS ########################### DATABASE CONFIG ###########################I have also added all of these variables to the “Environment Variables” section in Railway to ensure they are correctly configured.Could you please confirm whether this setup is correct or advise if any changes are needed to avoid the egress warning?Thank you for your help.

2 months ago

you are using the private network so you wont face any egress charges, the warning is only for when you use the public url i.e. the DB_HOST does not have a .internal suffix


kamkara-civ

Hello,I’m sharing my custom database configuration, which has been working correctly for the past six months. However, I may have made an error, so I’m providing it again for review.I’ve noticed the following warning on the DATABASE_PUBLIC_URL field:This variable references a public endpoint through this variable:DATABASE_PUBLIC_URL → RAILWAY_TCP_PROXY_DOMAINConnecting to a public endpoint will incur egress fees. That might happen if this variable, DATABASE_PUBLIC_URL, is used to establish a connection to a database or another service.You can avoid the egress fees by switching to a private endpoint (e.g., RAILWAY_PRIVATE_DOMAIN). Check out our documentation for more information!Production configuration (config/database.yml):production: primary: &primary_production <<: *default url: <%= ENV["DB_URL"] %> database: <%= ENV["DB_NAME"] %> username: <%= ENV["DB_USERNAME"] %> password: <%= ENV["DB_PASSWORD"] %> host: <%= ENV["DB_HOST"] %> port: <%= ENV["DB_PORT"] %>Credentials contents (config/credentials.yml.enc, production section):########################### DATABASE CONFIG ########################### production: DB_URL: postgresql://postgres:vvlvqzMzhvGBWgHkrAwQQsvDqtpdMFKS@postgres.railway.internal:5432/railway DB_NAME: railway DB_HOST: postgres.railway.internal DB_PORT: 5432 DB_USERNAME: postgres DB_PASSWORD: vvlvqzMzhvGBWgHkrAwQQsvDqtpdMFKS ########################### DATABASE CONFIG ###########################I have also added all of these variables to the “Environment Variables” section in Railway to ensure they are correctly configured.Could you please confirm whether this setup is correct or advise if any changes are needed to avoid the egress warning?Thank you for your help.

smolpaw
HOBBYTop 5% Contributor

2 months ago

That's a warning from railway about a public url being present in your postgres service.
To get rid of the warning remove the DATABASE_PUBLIC_URL from your variables then go to Settings -> Public Networking and click the trash icon to remove the public tcp proxy all together.
Doing this will make your db inaccessible from outside your project which is recommended. This improves security.

You are already using the private url railway.internal so you will not incur any egress charges.


smolpaw

That's a warning from railway about a public url being present in your postgres service.To get rid of the warning remove the DATABASE_PUBLIC_URL from your variables then go to Settings -> Public Networking and click the trash icon to remove the public tcp proxy all together.Doing this will make your db inaccessible from outside your project which is recommended. This improves security.You are already using the private url railway.internal so you will not incur any egress charges.

2 months ago

i will recommend he keeps the public url in case he wants to connect to it from pgadmin, it wont incur any costs even if he keeps it there as long as he does not use it


kamkara-civ

Hello,I’m sharing my custom database configuration, which has been working correctly for the past six months. However, I may have made an error, so I’m providing it again for review.I’ve noticed the following warning on the DATABASE_PUBLIC_URL field:This variable references a public endpoint through this variable:DATABASE_PUBLIC_URL → RAILWAY_TCP_PROXY_DOMAINConnecting to a public endpoint will incur egress fees. That might happen if this variable, DATABASE_PUBLIC_URL, is used to establish a connection to a database or another service.You can avoid the egress fees by switching to a private endpoint (e.g., RAILWAY_PRIVATE_DOMAIN). Check out our documentation for more information!Production configuration (config/database.yml):production: primary: &primary_production <<: *default url: <%= ENV["DB_URL"] %> database: <%= ENV["DB_NAME"] %> username: <%= ENV["DB_USERNAME"] %> password: <%= ENV["DB_PASSWORD"] %> host: <%= ENV["DB_HOST"] %> port: <%= ENV["DB_PORT"] %>Credentials contents (config/credentials.yml.enc, production section):########################### DATABASE CONFIG ########################### production: DB_URL: postgresql://postgres:vvlvqzMzhvGBWgHkrAwQQsvDqtpdMFKS@postgres.railway.internal:5432/railway DB_NAME: railway DB_HOST: postgres.railway.internal DB_PORT: 5432 DB_USERNAME: postgres DB_PASSWORD: vvlvqzMzhvGBWgHkrAwQQsvDqtpdMFKS ########################### DATABASE CONFIG ###########################I have also added all of these variables to the “Environment Variables” section in Railway to ensure they are correctly configured.Could you please confirm whether this setup is correct or advise if any changes are needed to avoid the egress warning?Thank you for your help.

smolpaw
HOBBYTop 5% Contributor

2 months ago

Never post your db url and secrets in full like you have done twice in this thread already. even for private databases.
Always redact them.


angobello

i will recommend he keeps the public url in case he wants to connect to it from pgadmin, it wont incur any costs even if he keeps it there as long as he does not use it

smolpaw
HOBBYTop 5% Contributor

2 months ago

For a development db sure. but for a production it's a big no no to have db exposed to public.
When and if you need to publicly access it you can do so very easily by going back to Settings -> Public networking, then remove the public url once done with it.
I love how easy railway makes this. just couple of clicks and you got a url.


smolpaw

For a development db sure. but for a production it's a big no no to have db exposed to public.When and if you need to publicly access it you can do so very easily by going back to Settings -> Public networking, then remove the public url once done with it.I love how easy railway makes this. just couple of clicks and you got a url.

kamkara-civ
FREE

2 months ago

Indeed, you’re right.

I’ve submitted everything, and once the issue is resolved,

I plan to delete the application and start everything over.


kamkara-civ

Indeed, you’re right.I’ve submitted everything, and once the issue is resolved,I plan to delete the application and start everything over.

smolpaw
HOBBYTop 5% Contributor

2 months ago

@kamkara-civ, if the issue has been resolved please mark this bounty as solved as well.
Thanks.


smolpaw

@kamkara-civ, if the issue has been resolved please mark this bounty as solved as well.Thanks.

kamkara-civ
FREE

2 months ago

Unfortunately not—the 502 error persists. I still cannot access the database.

https://applnclassapp-production.up.railway.app/


kamkara-civ

Unfortunately not—the 502 error persists. I still cannot access the database.https://applnclassapp-production.up.railway.app/

smolpaw
HOBBYTop 5% Contributor

2 months ago

Is your postgres still going into recovery mode ?


kamkara-civ
FREE

2 months ago

not


samnymr
PRO

2 months ago

Have you set the variable RAILS_ENV to production on the server?


kamkara-civ

not

smolpaw
HOBBYTop 5% Contributor

2 months ago

I am able to access it perfectly fine. What is the issue ?
If i can get some more details about the application and it's failure logs then i could assist you better.


kamkara-civ

Unfortunately not—the 502 error persists. I still cannot access the database.https://applnclassapp-production.up.railway.app/

smolpaw
HOBBYTop 5% Contributor

2 months ago

Hi kamkara, are you still facing the 502 issues ?
I see that the link is no longer valid. did you start over ?


Status changed to Solved chandrika about 1 month ago