Railway: Internal PostgreSQL Networking Breaks After Every Deploy
307163608
HOBBYOP

3 months ago

Hi,

I'm having a recurring problem after merging a PR into main, which triggers an automatic deployment on Railway.

The issue:
After the deployment finishes, the backend suddenly loses the connection to the PostgreSQL database. It can no longer connect internally from the Railway service — the internal connection string just stops working.

What’s strange is that the external PostgreSQL connection continues to work fine, only the internal Railway connection breaks.

The only workaround that helps is:

  1. Deleting the entire PostgreSQL instance

  2. Re-creating a new one

  3. Re-adding all environment variables manually

But this causes me to lose all data every time, which is not sustainable.

This looks like a Railway bug or networking issue, because nothing changes in my code — the deployment alone breaks the internal DB connection.

Could you advise on how to fix this or why internal connections stop working after deploy?

Thank you.

$10 Bounty

20 Replies

3 months ago

When you say the connection just stops working after deployment, what error(s) are you seeing?


crisog

When you say the connection just stops working after deployment, what error(s) are you seeing?

307163608
HOBBYOP

3 months ago

Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
Caused by: java.net.SocketTimeoutException: Connect timed out

I can only log in using external authentication; internal authentication no longer works. Everything runs inside Railway. The only thing that makes internal login work again is a hard reset — deleting the database and redeploying it from scratch — but this is not normal.


ilyassbreth
FREE

3 months ago

are both services in the same project? and which database url variable are you using exactly?


307163608
HOBBYOP

3 months ago

Yes, everything is in a single project, because everything works correctly until the first merge. After the first merge, internal loses connectivity.

If I push changes directly to the main branch and deploy from there, internal conenction does not stop working after deployment.

It feels like magic.

I’m using PostgreSQL with postgres.railway.internal as the PGHOST.


ilyassbreth
FREE

3 months ago

when you say "merge", you mean merging a pr into main via github, or merging locally then pushing?


ilyassbreth

when you say "merge", you mean merging a pr into main via github, or merging locally then pushing?

307163608
HOBBYOP

3 months ago

By “merge,” I mean merging a pull request into main via GitHub. After that, Railway triggers an automatic deploy of the service, and that’s when the connectivity to the internal PostgreSQL (postgres.railway.internal) is lost.


ilyassbreth
FREE

3 months ago

okay , clarify this two qsts , do you have pr environments enabled in railway? (settings -> environments) ?? and when the pr merges to main, is railway deploying to your main/production environment or creating a pr environment?


ilyassbreth
FREE

3 months ago

also are you using variable references like ${{Postgres.DATABASE_URL}} or hardcoded values in your backend service variables?


ilyassbreth

also are you using variable references like ${{Postgres.DATABASE_URL}} or hardcoded values in your backend service variables?

307163608
HOBBYOP

3 months ago

references


ilyassbreth

okay , clarify this two qsts , do you have pr environments enabled in railway? (settings -> environments) ?? and when the pr merges to main, is railway deploying to your main/production environment or creating a pr environment?

307163608
HOBBYOP

3 months ago

I have the following setup in Railway:

“Branch connected to test”
Changes made to this GitHub branch will be automatically pushed to this environment: main.

The flow is:

  • A PR is created in GitHub.

  • Nothing is deployed while the PR is open.

  • Only when the PR is merged into main, Railway triggers an automatic deployment.

To answer your questions:

  • PR environments are NOT enabled in Railway (Settings → Environments).

  • When the PR is merged into main, Railway deploys to the main/production environment, it does not create a PR environment.


ilyassbreth
FREE

3 months ago

okay so try this first, instead of using postgres.railway.internal directly as PGHOST, use the full railway variable reference ${{Postgres.DATABASE_URL}} ; let railway construct the full connection string. this ensures you're always getting the correct internal address


ilyassbreth
FREE

3 months ago

the issue is likely a deployment timing/ordering problem when pr merges trigger the deploy


ilyassbreth

okay so try this first, instead of using postgres.railway.internal directly as PGHOST, use the full railway variable reference ${{Postgres.DATABASE_URL}} ; let railway construct the full connection string. this ensures you're always getting the correct internal address

307163608
HOBBYOP

3 months ago

yes I have this reference ${{Postgres.DATABASE_URL}} no hardcoded value


ilyassbreth
FREE

3 months ago

okay wait, are you running database migrations or any database connections during BUILD time (like in a Procfile or nixpacks build step)?


ilyassbreth
FREE

3 months ago

check if you have, migrations in start command or db connections in build scripts or anything anything db-related before runtime


ilyassbreth

okay wait, are you running database migrations or any database connections during BUILD time (like in a Procfile or nixpacks build step)?

307163608
HOBBYOP

3 months ago

I’m using Flyway via Spring Boot auto-configuration only (Flyway dependency on the classpath).
I’m not running flyway:migrate manually, there is no Procfile, and no custom nixpacks build steps touching the database.

As far as I can see, Flyway runs only at application startup (runtime), not during the build phase.


ilyassbreth
FREE

3 months ago

okay add this to your railway backend service environment variables :

SPRING_FLYWAY_CONNECT_RETRIES=60

ilyassbreth
FREE

3 months ago

adding retries gives it 60 attempts (60 seconds) to establish connection, which should be more than enough for railway's internal network to stabilize after deployment


ilyassbreth
FREE

3 months ago

any update?


petrf22
HOBBY

2 months ago

I'm having the same issue. After updating the application, the database connection stops working for me. Neither restarting nor redeploying helped. My workaround is to add a nonsensical variable to the application settings in the "Variables" tab, which forces a redeployment of the application, and then everything starts working again.


Loading...