Adding a Database to our Railway App
Last time I looked at Railway, I got it up and running with a Blazor WASM app. Now, I'll look at adding a PostgreSQL database to it.
Last time I looked at Railway, I got it up and running with a Blazor WASM app. Now, I'll look at adding a PostgreSQL database to it. As with the app I got working, Railway's interface will make it incredibly simple to provision the database, and we'll need to do minimal work to get the connection info to our app.
Provisioning the Database #
From the Railway dashboard, you can click into your project, and there should be a New button at the top (as in a buttno that says "New", not a button whose appearance would not heretofore be expected):
This brings up the familiar dialog to provision resources, from which we'll select Database:
And then I'm going to choose PostgreSQL here. Note that if Railway doesn't have an option here for the database you want, you can always create a plain Docker image from an image of your preferred database.
And look at that, it deploys a new PostgreSQL database! This is one of the things I love about Railway - their interface to set up resources has eliminated all of the steps to setup that aren't 100% necessary, and the default settings they choose are logical and easily overwritten later if we need.
Notice also the attached pgdata
volume. As discussed before, Railway just stores all of your resources in Docker images. This doesn't require that you create a dockerfile
for each of your projects and resources, they can do that for you if you don't want to. It is to say that they give you all the tools you might want to use to be able to manage your resources as docker containers and volumes, so you can more or less choose the level of control you need over your resources.
The way I use Railway, I let it manage all my resources for me, and I don't bog myself down in the weeds of Docker as much. I've got plenty of time in my day job to get frustrated with Docker!
Connecting to our Database #
If you click on your new database resource and then the Variables
tab, you should see a bunch of variables:
This contains all the information that our app will need to connect to it. Railway allows the resources within a project to reference each others' variables, so we just need to know which ones to reference to build a connection string.
Now, they've all got incredibly obvious names so you can certainly accomplish this by guessing in as much time as it takes you to read this article, but for thoroughness' sake I've got a connection string here:
User Id=${{Postgres.PGUSER}};Password=${{Postgres.PGPASSWORD}};Host=${{Postgres.PGHOST}};Port=${{Postgres.PGPORT}};Database=${{Postgres.PGDATABASE}}
If you copy that as the value of a new variable in your application, it will fill in each of the references with the values from your database's variables, and that should be a sufficient connectino string to test that it works:
Note that I'm using the variable name ConnectionStrings__Database
, with two underscores, which behaves as though I'm inserting the following into my appsettings
(in a .NET context):
"ConnectionStrings": { "Database": "..."}
And that's all the configuration needed for your app to be able to consume your new database!