7 months ago
I have a laravel app with a postgres db service. Build commands can't connect to the db.
I have these variable settings in the laravel service
DB_CONNECTION="pgsql"
DB_URL="${{Postgres.DATABASE_URL}}"
And see these errors: for my build-app.sh
SQLSTATE[08006] [7] could not translate host name "postgres.railway.internal" to address: Name or service not known (Connection: pgsql, SQL: delete from "cache")
In fact no matter what I try every connection fails. I tried with a mysql db and had similar errors.
Im following these instructions
https://docs.railway.com/guides/laravel#deploy-from-a-github-repo
Can you help
Pinned Solution
7 months ago
I think @error suggestion is wrong here and most of his/her replies coming from chatgpt.
It does have access to the env that's why you see the "postgres.railway.internal" in the build logs
What you need to understand is that the build environments are isolated from your runtime services so it cannot connect to pg using private network
There are couple of ways to fix this,
1. Don't do db operations during build time and move them to runtime. If it's migrations you need to do then move them to a pre-deploy command, more on this here: https://docs.railway.com/guides/pre-deploy-command
2. Attach a public endpoint to your db which you can access during build time but use only the private endpoint during runtime
4 Replies
7 months ago
It looks like during build time it doesnt have access to the runtime variable Postgres.DATABASE_URL. Try temporarily setting it manually to the private/public url to debug and see if it fixes
7 months ago
I think @error suggestion is wrong here and most of his/her replies coming from chatgpt.
It does have access to the env that's why you see the "postgres.railway.internal" in the build logs
What you need to understand is that the build environments are isolated from your runtime services so it cannot connect to pg using private network
There are couple of ways to fix this,
1. Don't do db operations during build time and move them to runtime. If it's migrations you need to do then move them to a pre-deploy command, more on this here: https://docs.railway.com/guides/pre-deploy-command
2. Attach a public endpoint to your db which you can access during build time but use only the private endpoint during runtime
smolpaw
I think @error suggestion is wrong here and most of his/her replies coming from chatgpt.It does have access to the env that's why you see the "postgres.railway.internal" in the build logsWhat you need to understand is that the build environments are isolated from your runtime services so it cannot connect to pg using private networkThere are couple of ways to fix this,1. Don't do db operations during build time and move them to runtime. If it's migrations you need to do then move them to a pre-deploy command, more on this here: https://docs.railway.com/guides/pre-deploy-command2. Attach a public endpoint to your db which you can access during build time but use only the private endpoint during runtime
7 months ago
Most of my replies are not coming from chatgpt i am just suggesting another method of ruling out variables 
7 months ago
if i may ask what is DB_CONNECTION used for?
AFAIK the only variable you need to connect a postgres service with your app is the DATABASE_URL the internal not the public one because the last one will incur egress charges!
it is ok for a quick test but make sure to switch back
a quick way to check if your db is up and running is to click on the postgres service and navigate to the Data tab, if you can see the db tables that means it is working
also what kind of commands are trying to run on build time that an active db connection?
Status changed to Solved chandrika • 7 months ago
