Best practices for running one time scripts
toothlessll
FREEOP

8 months 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

8 months ago

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


8 months 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

8 months 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


8 months 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

8 months 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

1395878295039381500


8 months ago

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


toothlessll
FREEOP

8 months ago

I am using database public url


8 months ago

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


toothlessll
FREEOP

8 months ago

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


toothlessll
FREEOP

8 months ago

I have not manually changed any postgres settings either


longbuivan
HOBBY

8 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.


Loading...