Private internal vs public URLs between services.
affanisautomated
PROOP

8 months ago

I've deployed a pgvector template (Brody's template of pgvector db) for my database and in my backend service, I've set DATABASE_URL=${{pgvector.DATABASE_URL}}

This works but it tells me that I should use private domain or incur egress fees. However, when I swap it out to ${{pgvector.DATABASE_URL_PRIVATE}}, my backend is unable to connect to the database and gives me:
Can't react database server at pgvector.railway.internal:5432

Project ID: c243ded6-9e79-4752-857d-5b6f31fd8cc2

$10 Bounty

4 Replies

asim1801
FREE

8 months ago

You are running into this issue because Railway provides two different types of service URLs: public and private. The public URL (like pgvector.up.railway.app) is accessible from anywhere, including outside Railway, but using it for service to service communication can lead to egress fees. The private URL (like pgvector.railway.internal) is meant to be used internally between services deployed within the same Railway project it avoids those fees and is generally faster. When you switched to using DATABASE_URL_PRIVATE, your backend couldn't connect because the private .railway.internal domain is only resolvable inside Railway’s own infrastructure. If you're testing or running your backend service locally, that internal hostname won't work, because your local environment doesn't have access to Railway’s internal DNS. To fix this, make sure your backend service and the pgvector database are deployed in the same Railway project. Then, use the private database URL(DATABASE_URL_PRIVATE) only in the Railway deployment environment, not when running the app locally. For local development, continue using the public DATABASE_URL. You can manage this easily by defining environment specific variables in Railway’s environment settings, so that local and production environments use the correct database URL automatically. Once your backend is deployed inside Railway and using the private URL, it should connect without any issues and avoid egress costs.

To fix this, make sure your backend service and the pgvector database are deployed in the same Railway project. Then, use the private database URL (DATABASE_URL_PRIVATE) only in the Railway deployment environment, not when running the app locally. For local development, continue using the public DATABASE_URL. You can manage this easily by defining environment specific variables in Railway’s environment settings, so that local and production environments use the correct database URL automatically. Once your backend is deployed inside Railway and using the private URL, it should connect without any issues and avoid egress costs.


phoenixauro
HOBBY

8 months ago

your backend service needs to be deployed on railway and be in the same project or region to work. In other cases you need to use the public url


asim1801

You are running into this issue because Railway provides two different types of service URLs: public and private. The public URL (like pgvector.up.railway.app) is accessible from anywhere, including outside Railway, but using it for service to service communication can lead to egress fees. The private URL (like pgvector.railway.internal) is meant to be used internally between services deployed within the same Railway project it avoids those fees and is generally faster. When you switched to using DATABASE_URL_PRIVATE, your backend couldn't connect because the private .railway.internal domain is only resolvable inside Railway’s own infrastructure. If you're testing or running your backend service locally, that internal hostname won't work, because your local environment doesn't have access to Railway’s internal DNS. To fix this, make sure your backend service and the pgvector database are deployed in the same Railway project. Then, use the private database URL(DATABASE_URL_PRIVATE) only in the Railway deployment environment, not when running the app locally. For local development, continue using the public DATABASE_URL. You can manage this easily by defining environment specific variables in Railway’s environment settings, so that local and production environments use the correct database URL automatically. Once your backend is deployed inside Railway and using the private URL, it should connect without any issues and avoid egress costs.To fix this, make sure your backend service and the pgvector database are deployed in the same Railway project. Then, use the private database URL (DATABASE_URL_PRIVATE) only in the Railway deployment environment, not when running the app locally. For local development, continue using the public DATABASE_URL. You can manage this easily by defining environment specific variables in Railway’s environment settings, so that local and production environments use the correct database URL automatically. Once your backend is deployed inside Railway and using the private URL, it should connect without any issues and avoid egress costs.

affanisautomated
PROOP

8 months ago

Thank you for your comment.

All of my services are running on railway. I have 3 services, a pgvector database (Brody's template that's available), a NestJS backend and a React frontend. All 3 of these are deployed on Railway in the same project.


8 months ago

Hello,

Brody here,

If you are getting that error during build: the private network is not available during build, you would need to move your database actions into a pre-deploy command or do them during runtime.

Best,

Brody


Loading...