2 months ago
I am trying to get my stage environment to use the same Postgres storage as my PROD. But it will not connect. It is in the same project and I have training delating the variables and adding them back, also sharing the databases.
Please help.
4 Replies
2 months ago
This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.
Status changed to Open Railway • 2 months ago
2 months ago
Can't you use the public endpoint of your production db for that? Not something I would recommend but still...
2 months ago
From my understanding, it sounds like you're trying to have your staging backend use the same database as production.
Railway mentions environments are “isolated instances of all services,” which might mean internal networking doesn’t work across staging/prod — I haven’t personally tested that though.
I'd try:
-Check logs + connection settings first — see if the request is actually reaching the DB. If it is but failing, it could be limits or config (PgBouncer might help in that case)
-If it’s not reaching at all, try using the public DATABASE_URL from prod instead
2 months ago
Why it’s not connecting
Most likely causes:
- Your staging service does not have
DATABASE_URLset - You didn’t link the Postgres service variable properly
- You’re using a different environment (staging vs production) without shared variables
The correct way to share the database (Railway)
Step 1 — Go to your PostgreSQL service
- Open your Railway project
- Click on your Postgres service
- Go to Variables or Connect
You will see something like:
DATABASE_URL = postgresql://postgres:Your_Password@Identifiant.co:5432/dbStep 2 — Link it to your staging service
Go to your backend (staging) service → Variables
Add:
DATABASE_URL = ${{ Postgres.DATABASE_URL }}IMPORTANT:
Postgres= exact name of your database service- This links dynamically to the same DB
Step 3 — Redeploy
After adding the variable:
Click Deploy → Redeploy
(or push a commit)
Optional (recommended)
If you want BOTH environments to share the same DB:
- Use the same
DATABASE_URLin:- Production service
- Staging service
Test it
After deploy:
- Check logs → you should see:
PostgreSQL connected- Try an API call → data should match PROD
⚠ Common mistakes
ProblemFixUsing .env locallyRailway ignores it → use Variables tabTypo in ${{ Postgres.DATABASE_URL }}Must match service name exactlyForgot redeployAlways redeploy after adding variablesUsing different DBMake sure both point to same URL
2 months ago
How to make the staging environment and production environment use the same PostgreSQL database in a Railway project
For your project's staging environment and production environment use the same Postgres database service, your staging environment will have to use the public URL of your production environment's Postgres database service. I outline the procedure in the steps below.
- In your project canvas, click the Environments dropdown in the top left corner. Select
productionunder Environments. - Select the Postgres service.
- Select the Variables tab.
- Look for the
DATABASE_PUBLIC_URLvariable. Hover over it. Click the Copy icon to copy theDATABASE_PUBLIC_URLvalue. - Click the Environments dropdown in the top left corner. Select
stagingunder Environments. - Select the service that will connect to the Postgres service.
- Select the Variables tab.
- Look for the variable that uses the public URL of the Postgres service. Hover over the variable. Click the ⋮ vertical ellipsis icon.
- Click Edit.
- Delete the current value.
- Paste in the value of
DATABASE_PUBLIC_URL. The one you copied from the production environment. - Click ✓ to save.
- Click Deploy.
- Wait for your service to build and be deployed then test to see if your staging service is using the same database as your production service.
---
I believe your request to make the both the staging environment and the production environment use the same database has been fulfilled using the guide I have provided.
If you have any questions, feel free to ask.
If you believe that this answer is satisfactory, please approve.