Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Download Railway database to localhost
Michal Bryxí
Michal Bryxí

Posted on

     

Download Railway database to localhost

For my hobby projects I like to download live, production db and load it on my localhost environment so that I can quickly explore data or test potentially dangerous migrations.


Assumptions

This post assumes your hosting is onRailway, and you usePostgresql as a database.

Optional, but I'd also assume that you usedotenv for yourRuby on Rails backend and your app is running locally onDocker.

Credentials

In my app I have two env files./backend/.env.development:

# localhostPGDATABASE=some-local-databasePGUSER=postgresPGPASSWORD=somepasswordPGHOST=dbPGPORT=5432# docker:db container defaultsPOSTGRES_PASSWORD=${PGPASSWORD}
Enter fullscreen modeExit fullscreen mode

Thanks to this config, my application will be able to connect to local database using the same set ofenvironment variables as Railway.

My Rails db config then looks likebackend/config/database.yml:

default:&defaultadapter:postgresqlpool:<%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>timeout:5000encoding:unicodedatabase:<%= ENV["PGDATABASE"] %>username:<%= ENV["PGUSER"] %>password:<%= ENV["PGPASSWORD"] %>host:<%= ENV["PGHOST"] %>production:<<:*defaultdevelopment:<<:*defaulttest:<<:*defaultdatabase:some_app_test
Enter fullscreen modeExit fullscreen mode

The aliasing ofPOSTGRESQL_PASSWORD then takes advantage ofdefault Postgresql Docker setup which will then set up respective user on container creation.

One thing to note is thatRailway uses TimescaleDB so yourdocker-compose.yml will need to:

services:  db:    env_file:       - ./backend/.env.development    image: timescale/timescaledb-postgis:latest-pg13
Enter fullscreen modeExit fullscreen mode

Second file looks very similar./backend/.env.production:

# railway.appPGDATABASE=some-railway-databasePGUSER=postgresPGHOST=some-railway-url.railway.appPGPORT=1234
Enter fullscreen modeExit fullscreen mode

You need to fill this one with credentials found in yourRailway -> PostgreSQL -> Variables.

The script

Now for the actual dump/download/load I'm using following script./bin/database_pull.sh:

#!/bin/bashDUMP_FILENAME='./some-app.com_dump.sql'source ./backend/.env.production||exit$?echo'💾 Creating production dump'pg_dump-U$PGUSER-h$PGHOST-p$PGPORT-W-F t$PGDATABASE>$DUMP_FILENAMEsource ./backend/.env.development||exit$?echo'🎡 Loading dump on localhost'dropdb$PGDATABASE-p$PGPORT-U$PGUSER-h$PGHOSTcreatedb$PGDATABASE-p$PGPORT-U$PGUSER-h$PGHOSTpg_restore-U$PGUSER-h$PGHOST-p$PGPORT-W-F t-d$PGDATABASE$DUMP_FILENAMEecho'✨ Success!'
Enter fullscreen modeExit fullscreen mode

This scriptneeds to be provided the values of${PGPASSWORD} manually. You have to provideproduction first andlocal second:

./bin/database_pull.sh💾 Creating production dumpPassword:🎡 Loading dump on localhostPassword:✨ Success!
Enter fullscreen modeExit fullscreen mode

Conclusion

Developer Experience is important part of every developer life. Being able to use standard configuration and run one command to get my hands on the data I need in the format I prefer is IMO a huge DX improvement.


Image generated by Midjourney prompt:railway hosting; blog post; downloading database; coding; ruby on rails --ar 16:9

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
vatana7 profile image
vatana7
a bit of everything
  • Email
  • Location
    Cambodia, Phnom Penh
  • Education
    Kirirom Institute Of Technology
  • Work
    Software Engineer at Peppermint Cambodia
  • Joined

very helpful!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Cycle 🚴 , climb 🗻 , run 🏃 , travel 🌍 , enjoy life ♥.IT guy with the need to live fully.
  • Location
    Interlaken, Switzerland
  • Education
    University of West Bohemia
  • Pronouns
    he/him
  • Work
    Sandcastle Architect
  • Joined

More fromMichal Bryxí

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp