Cannot access postgres.railway.internal and Rails build fails on migrate

a year ago

tl;dr During the build phase, Postgres' private URL (postgres.railway.internal) cannot be resolved so rails db:migrate fails. Using the public proxied URL works fine.

I recently created a Postgres database and connected it to my web and worker instances. I tried to use ${{Postgres.DATABASE_PRIVATE_URL}} but when I do I get the following error

ActiveRecord::NoDatabaseError: We could not find your database: railway. Which can be found in the database configuration file located at config/database.yml. (ActiveRecord::NoDatabaseError)
…
Caused by:
PG::ConnectionBad: could not translate host name "postgres.railway.internal" to address: Name or service not known (PG::ConnectionBad)

When I use ${{Postgres.DATABASE_URL}} it works fine.

I've looked through a couple of question threads and have tried the following to no success:

  1. ENABLE_ALPINE_PRIVATE_NETWORKING=true as a variable

  2. Adding sleep 3 to before rails db:migrate via Settings => Custom Build Command

7 Replies

a year ago

the private network is not available during the build phase.

https://docs.railway.app/reference/private-networking#caveats

use the public url when migrating during build, or use the private url and do migrations at runtime.


a year ago

I'd appreciate any advice on how to use ${{Postgres.DATABASE_URL}} during build and ${{Postgres.DATABASE_PRIVATE_URL}} at runtime.

I'm also at a loss how to skip the migrations during build since the Dockerfile it appears it's running from isn't in my repo.


a year ago

I'd appreciate any advice on how to use ${{Postgres.DATABASEURL}} during build and ${{Postgres.DATABASEPRIVATE_URL}} at runtime.

I'm not a rails dev, so I would be no help there.

I'm also at a loss how to skip the migrations during build since the Dockerfile it appears it's running from isn't in my repo.

but you set the command yourself?

3 .Adding sleep 3 to before rails db:migrate via Settings => Custom Build Command


a year ago

I found I was able to set a custom build command that would get injected into the build process the Dockerfile was using from the web service's menu Settings => Build => Custom Build Command (see attached screenshot). When I put sleep 3 in here, it would run before rails db:migrate from the injected Dockerfile.

My hunch is the most straightforward solution is creating my own Dockerfile for this app. Then I can overwrite the env var in the build phase like

DATABASE_URL=$PUBLIC_DB_URL rails db:migrate

with the following variables

DATABASE_URL=${{Postgres.DATABASE_PRIVATE_URL}}
PUBLIC_DB_URL=${{Postgres.DATABASE_URL}}

I'll give this a shot when I have a moment but do let me know your thoughts or if this is commonplace with Rails and Postgres on Railway.

Attachments


a year ago

as mentioned previously, you could also not run a migration during build, instead run the migration during runtime before you start your app, a dockerfile is not needed for that


a year ago

Absolutely true! The Dockerfile that ships w/ Rails (starting in 7.1) actually handles this in bin/docker-entrypoint by running ./bin/rails db:prepare if it's being started as the web server (https://github.com/rails/rails/blob/89adf24a570b0adf9f62011339b042046471f421/railties/lib/rails/generators/rails/app/templates/docker-entrypoint.tt#L10-L12 for curious folks) so it's probably the recommended way to do this.

In my particular instance however, I believe I need to create a Dockerfile regardless. I am quite new to Railway so this may not be correct and I'd love any corrections. My understanding is that w/o a Dockerfile, one is injected for me perhaps from Nixpacks. I see the following in my build log (spacing removed)

Using Nixpacks
#0 building with "default" instance using docker driver
#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 4.92kB done

Unless I'm somehow able to alter this file I don't think I can otherwise stop it from running rails db:migrating during build.


guillaumeocculyHOBBY

8 months ago

I have the same problem as you without having found a solution.

My environments variables are referenced properly and I can see the values on the rails console.

But I still have the issue when I want to create the database or migrate.

Attachments