HOSTING / LESSON 06
Adding a Database.
Provisioning PostgreSQL on Railway

Loading the lesson…
Loading activities…
HOSTING / LESSON 06
Provisioning PostgreSQL on Railway

Loading the lesson…
Loading activities…
Almost every app has to remember stuff. User accounts, posts, orders, settings. All of that data needs a safe place to live, even when your app restarts. That's the database's whole job.
In this lesson you'll add a database to your Railway project and hook it up to your app.
A database is organized storage for your app's data. Picture a super-powered spreadsheet that can do all this.
There are a ton of database types out there. We're going with PostgreSQL (most people just say "Postgres"). Here's why.
Railway makes adding PostgreSQL ridiculously easy. A few clicks and you've got a running database.
Let's add a database to the project you deployed last lesson.
Go to your Railway dashboard and click the project you created.
Click the "New" button inside your project, or right-click on the empty space. You'll see options for adding services.
Pick "Database" from the options. Railway shows you a few database types.
Click "PostgreSQL." Railway starts building your database right away.
Railway takes about 30 seconds to set things up. A new card pops up in your project for the PostgreSQL service.
Click the PostgreSQL service card to see more details. Here's what's in there.
Railway shows you everything you need to connect.
You can view and edit your data right inside Railway. It's a simple data browser that shows your tables.
The connection details also show up as environment variables. That's how your app is going to connect.
Your database exists now, but your app has no clue about it yet. You have to hand your app the connection info.
Railway makes that easy with something called "Reference Variables."
In your project, click the service running your web app (the app, NOT the PostgreSQL service).
Click the "Variables" tab.
Click "New Variable" and look for the option to reference another service. You can also type it in by hand, using Railway's reference syntax with your database service's name.
Railway can fill in the connection string for you. Find the PostgreSQL service and pick its DATABASE_URL variable.
The DATABASE_URL squishes all the connection details into one string. It looks like this.
Railway stages the new variable instead of applying it right away. Click "Deploy" on the banner at the top of your project, and Railway redeploys your app with the new settings.
Your app can now reach the database through the DATABASE_URL environment variable. How you use it depends on your language and framework.
Prisma gets its connection string from DATABASE_URL too (the Prisma lesson in the Databases module walks through the setup). On Railway you don't need a .env file at all, because the variable you just added gets handed to your app at runtime. Your local .env file only holds the connection string for the database you use while developing.
So how do you know your app is actually connected? Here are a few ways to check.
Look for connection success or error messages when your app starts up.
If your app has a sign-up form or saves data somehow, give it a spin. If it works, your database is connected.
Click your PostgreSQL service and go to the "Data" tab. After using your app a bit, you should see tables and data showing up here.
A few tips for working with databases on Railway.
Railway has a Backups tab on your database service where you can make backups by hand or schedule them daily, weekly or monthly. It's on you to turn that on though, so do it before you have data you'd cry about losing.
Keep your DATABASE_URL secret. Your password is sitting right inside it.
If you've got a testing environment and a production environment, give each one its own database.
Databases eat memory and storage. Keep an eye on the Resources tab so you don't run out.
Railway includes a simple way to peek at what's in your database.
Go to your PostgreSQL service in Railway.
Click "Data" to open the data browser.
If your app has created tables, you'll see them listed. Click a table to see what's inside.
You can also use outside tools like pgAdmin or TablePlus for fancier database management.
You've got an app with a database now. DATABASE_URL is just one example of an environment variable though. Next lesson, you'll dig into environment variables in more detail, including how to safely store secrets like API keys...