Transfer data from one app to another
avpatel18
PROOP

3 months ago

Dear Railway Team,

I have two similar project (one single tenant and one multitenant but same) deployed on railway and I want to transfer some postgres data from one app to another. what is the best way to do this? My idea was to get the json dump file and then upload to another, but I cant seem to find a way to download the data to my local machine. Is there a better or direct way to do this?

Thanks,

Ankit

Solved$20 Bounty

Pinned Solution

yusufmo1
PRO

2 months ago

the fact that DATABASE_URL is identical between production and staging means they're pointing to the same database, which is why your restore wiped production. when you create a new environment in railway, you need to add a separate postgres service to that environment (click + New in your staging environment and add postgres). each environment should have its own database instance with unique credentials.

once you have a separate staging postgres, use reference variables like ${{Postgres.DATABASE_URL}} in your app service instead of hardcoding the url. that way each environment automatically gets its own database connection. then you can safely pg_dump from production and pg_restore to staging without them touching each other.

if you need to recover the wiped production data, check if railway has automatic backups enabled for your postgres service (Settings > Backups). you might be able to restore from a recent snapshot.

6 Replies

3 months ago

The best way is to pg_dump and pg_restore manually.


Status changed to Awaiting User Response Railway 3 months ago


Status changed to Solved avpatel18 3 months ago


avpatel18
PROOP

2 months ago

Dear ray, could you please help me with this, I have a production app with postgreSQL db and I have created staging env, however the postgres data are empty on staging, I was trying to do pgdump and pgrestore as you suggested but it wiped all my data from production. I have looked at the DATABASE_URL and DATABASE_PUBLIC_URL and they are exactly same between the production and staging env, is there a way I can have an exact seperate copy of the production in staging? Thanks


Status changed to Awaiting Railway Response Railway 2 months ago


yusufmo1
PRO

2 months ago

the fact that DATABASE_URL is identical between production and staging means they're pointing to the same database, which is why your restore wiped production. when you create a new environment in railway, you need to add a separate postgres service to that environment (click + New in your staging environment and add postgres). each environment should have its own database instance with unique credentials.

once you have a separate staging postgres, use reference variables like ${{Postgres.DATABASE_URL}} in your app service instead of hardcoding the url. that way each environment automatically gets its own database connection. then you can safely pg_dump from production and pg_restore to staging without them touching each other.

if you need to recover the wiped production data, check if railway has automatic backups enabled for your postgres service (Settings > Backups). you might be able to restore from a recent snapshot.


avpatel18
PROOP

2 months ago

Thanks Yusuf for quick reply, yes I had daily backups so I was able to restore it. Could I please ask your help regarding pgdump and pgrestore, once I have seperate staging postgres instance, how exactly I can do pgdump and pgrestore, should I do this directly on railway or via ssh? Do I have to use DATABASE_URL or PUBLIC URL? thanks


yusufmo1
PRO

2 months ago

no worries!! you'll do this from your local machine using the PUBLIC_URL since you can't ssh into railway's managed postgres directly. grab the public connection details from both databases in the railway dashboard.

first dump your production data locally:

pg_dump "postgresql://user:pass@productionhost.proxy.rlwy.net:port/railway" > backup.sql

then restore to your staging database:

psql "postgresql://user:pass@staginghost.proxy.rlwy.net:port/railway" < backup.sql

replace the connection strings with your actual DATABASE_PUBLIC_URL values from each environment. let me know how it goes!


avpatel18
PROOP

2 months ago

Thanks Yusuf! you guys are awesome. Really appreciate your prompt response.


Status changed to Solved chandrika 2 months ago


Loading...