Railway PostgreSQL: "Connection Refused" with Internal URL, Public URL Works Fine
zfallan121
PROOP

2 months ago

I'm running a Python Flask app using Gunicorn and psycopg2 to connect to a PostgreSQL database hosted on Railway. When I use the public database URL, everything works perfectly. However, when I switch to the internal Railway connection string (host: osrs-fliphub.railway.internal, port: 5432), the app fails to connect and throws the following error:

  • connection to server at "osrs-fliphub.railway.internal" (10.128.89.54), port 5432 failed: Connection refused

    Is the server running on that host and accepting TCP/IP connections?

This happens during container startup, and the error repeats for both IPv4 and IPv6 addresses. My DATABASE_URL is set via environment variable, and the code uses psycopg2.connect(DATABASE_URL).

Why does the internal connection string fail, while the public one works? Is there a Railway-specific setting or networking rule I’m missing for internal connections? Any advice would be appreciated!

Solved$10 Bounty

Pinned Solution

darseen
HOBBYTop 5% Contributor

2 months ago

Railway's Private Network is primarily IPv6. While the internal DNS might resolve to both IPv4 (10.128.89.54) and IPv6 addresses, the database service might be listening on the IPv6 interface for internal connections.
Your error shows the app tried to connect via IPv4 (10.128.89.54) and failed. It likely also failed on IPv6 if your container environment isn't configured to prioritize or handle IPv6 correctly.

You need to ensure your application environment prefers or correctly handles IPv6.

2 Replies

darseen
HOBBYTop 5% Contributor

2 months ago

Railway's Private Network is primarily IPv6. While the internal DNS might resolve to both IPv4 (10.128.89.54) and IPv6 addresses, the database service might be listening on the IPv6 interface for internal connections.
Your error shows the app tried to connect via IPv4 (10.128.89.54) and failed. It likely also failed on IPv6 if your container environment isn't configured to prioritize or handle IPv6 correctly.

You need to ensure your application environment prefers or correctly handles IPv6.


darseen

Railway's Private Network is primarily IPv6. While the internal DNS might resolve to both IPv4 (10.128.89.54) and IPv6 addresses, the database service might be listening on the IPv6 interface for internal connections.Your error shows the app tried to connect via IPv4 (10.128.89.54) and failed. It likely also failed on IPv6 if your container environment isn't configured to prioritize or handle IPv6 correctly.You need to ensure your application environment prefers or correctly handles IPv6.

zfallan121
PROOP

a month ago

Worked ty!!


Status changed to Solved ray-chen about 2 months ago


Loading...