Best practices for running one time scripts
toothlessll
FREEOP

a year ago

Hello hi!

This is my first time using railway or any online hosting service. So please don’t mind the dumb question! But if I have a script that I want to execute only once (e.g. creating database tables or preloading into tables) what’s the best way to go about doing this? It doesn’t feel right putting it in build then removing it and also doesn’t feel right adding a service and a new github repository either.

Thanks in advance!

$10 Bounty

11 Replies

toothlessll
FREEOP

a year ago

885b1b4f-193a-43be-8a42-9edf0721a102


a year ago

Hello!

Welcome to Railway!

Since your example only mentioned database actions, I think this would be best achieved by using railway run locally.

This will allow you to run the script locally but with the service environment variables available.


toothlessll
FREEOP

a year ago

Ooh that is neat thank you :D

I’m also curious if you’re able to share the same volume with a different service? Primarily the filebrowser since sometimes I’d want to check the files inside manually


a year ago

We don't support shared services yet.

But if you just wanna look and manage the files, you can ssh in with railway ssh


toothlessll
FREEOP

a year ago

Oo thank you - I've been trying and testing railway run as you suggested, but running into getaddrinfo ENOTFOUND postgres.railway.internal error, but not sure what I'm doing wrong

1395878295039381575


a year ago

You would need to use the public database URL when running the command locally


toothlessll
FREEOP

a year ago

I am using database public url


a year ago

postgres.railway.internal is an internal URL, perhaps you have the code using the wrong variables?


toothlessll
FREEOP

a year ago

I'm not sure, I'm using postgres default variables, and referencing process.env.DATABASE_PUBLIC_URL


toothlessll
FREEOP

a year ago

I have not manually changed any postgres settings either


longbuivan
HOBBY

10 months ago

To run one time script like database init. I use either 2 ways:

1. railway cli to: railway ssh and run it

2. mount application location with volume (in Railway mount app/script) . create script folder under your root project. and run it via railway ssh or build command when you deploy.


Welcome!

Sign in to your Railway account to join the conversation.

Loading...