Postgres connection problems
codepawfect
PROOP

2 months ago

In some cases, modifying an environment variable and redeploying the service results in a failed connection attempt to the managed PostgreSQL database. The issue is only resolved after several redeployments.

Solved$20 Bounty

Pinned Solution

codepawfect
PROOP

2 months ago

Changing the DB URL from the hardcoded
jdbc:postgresql://postgres.railway.internal:5432/railway
to the environment variable-based
jdbc:postgresql://${{Postgres.PGHOST}}:${{Postgres.PGPORT}}/${{Postgres.PGDATABASE}}
seems to have fixed the connection issue.

I'm not sure why this should make a difference, but the deployment worked now about five times in a row. Previously, every deployment—or at least every other one—was failing.

10 Replies

bbhoss
FREE

2 months ago

I ran into the same issue recently. Not sure if it actually fixed it, but try ensuring that you're connecting using IPv6, that seemed to fix it for me. I got two deploys out. Previously I was getting connection errors. This is using Elixir Ecto but potentially could be the same underlying issue for you as well?


Railway
BOT

2 months ago


fra
HOBBYTop 10% Contributor

2 months ago

the db is not managed, when it doesn't connect, do you see any error? Are you using the internal or external url?


domehane
FREETop 10% Contributor

2 months ago

what framework/language are you using?


domehane
FREETop 10% Contributor

2 months ago

if you're using spring boot + hikari connection pool this is a timing race condition when your backend starts before postgres is ready. add these two lines to your application.properties:

spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.initialization-fail-timeout=-1

the -1 tells hikari to keep retrying indefinitely during startup instead of failing immediately. this fixed the exact same issue (50% deployment failures) for another user , they went from every 2nd deployment failing to zero failures. also make sure you're using the internal railway connection string with railway.internal not the public proxy url.

if not spring boot tell me which framework/language are you using?


domehane

if you're using spring boot + hikari connection pool this is a timing race condition when your backend starts before postgres is ready. add these two lines to your application.properties:spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.initialization-fail-timeout=-1the -1 tells hikari to keep retrying indefinitely during startup instead of failing immediately. this fixed the exact same issue (50% deployment failures) for another user , they went from every 2nd deployment failing to zero failures. also make sure you're using the internal railway connection string with railway.internal not the public proxy url.if not spring boot tell me which framework/language are you using?

codepawfect
PROOP

2 months ago

I’m using Spring Boot. However, the configuration you mentioned doesn’t resolve the issue, since these settings are already configured on my side.


domehane

what framework/language are you using?

codepawfect
PROOP

2 months ago

Spring Boot 4.0.1 + Java 25


fra

the db is not managed, when it doesn't connect, do you see any error? Are you using the internal or external url?

codepawfect
PROOP

2 months ago

There are no errors in the PostgreSQL logs. The database is up, running, and ready to accept connections.


bbhoss

I ran into the same issue recently. Not sure if it actually fixed it, but try ensuring that you're connecting using IPv6, that seemed to fix it for me. I got two deploys out. Previously I was getting connection errors. This is using Elixir Ecto but potentially could be the same underlying issue for you as well?

codepawfect
PROOP

2 months ago

This is more a workaround for an infrastructure/networking quirk. I’d rather see the Railway team resolve the underlying network and DNS problems.


codepawfect
PROOP

2 months ago

Changing the DB URL from the hardcoded
jdbc:postgresql://postgres.railway.internal:5432/railway
to the environment variable-based
jdbc:postgresql://${{Postgres.PGHOST}}:${{Postgres.PGPORT}}/${{Postgres.PGDATABASE}}
seems to have fixed the connection issue.

I'm not sure why this should make a difference, but the deployment worked now about five times in a row. Previously, every deployment—or at least every other one—was failing.


Status changed to Solved brody 2 months ago


Loading...