Using Railway’s Postgres Plugin for Local Development and CI
Learn how to spin up a managed Postgres instance with Railway’s CLI, wire it into your app locally and in CI, verify connectivity, and safely tear it down when done.
23 Sept 2026, 00:26 UTC

Desired outcome
You want a reproducible Postgres database that mirrors the production schema for both local development and automated CI tests, without managing servers or hard‑coding credentials.
Prerequisites
- A Railway account and a project created in the Railway dashboard.
- The Railway CLI installed (
npm i -g railwayor via your package manager). - Access to a terminal with permission to run CLI commands and execute
psql(PostgreSQL client). - For CI: ability to inject secrets (e.g., GitHub Actions secrets, GitLab CI variables) into the runner environment.
Procedure
1. Add the Postgres plugin to your Railway project
Run this command in the root of your repository:
railway add postgres
The CLI creates a Postgres service, stores its connection string as the environment variable RAILWAY_DB_URL, and links it to your project. No further configuration is required.
2. Start the database locally
From the same directory, launch the plugin:
railway up
This pulls the credentials from Railway, sets RAILWAY_DB_URL in your local shell, and provisions a temporary Postgres instance in the selected region (default is the project’s region).
3. Verify connectivity
Check that you can talk to the database:
psql $RAILWAY_DB_URL -c "SELECT 1;"
You should see a single row with the value 1. If the command fails, note the error and proceed to the verification section.
4. Configure your application
Most frameworks read a database URL from an environment variable. Ensure your app uses RAILWAY_DB_URL (or copies it to the variable your framework expects, e.g., DATABASE_URL). Example for a Node.js app:
// .env (do not commit)
DATABASE_URL=$RAILWAY_DB_URL
5. Run migrations or seed data (optional)
If you use a migration tool, invoke it now:
npx prisma migrate deploy # example with Prisma
This will apply schema changes to the temporary Postgres instance.
6. Use the database in CI
In your CI configuration, install the Railway CLI, authenticate with an API key stored as a secret, then bring up the database before running tests.
Example for GitHub Actions:
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
steps:
- uses: actions/checkout@v3
- name: Install Railway CLI
run: npm i -g railway
- name: Start Postgres
run: railway up
- name: Wait for DB readiness
run: |
for i in {1..10}; do
psql $RAILWAY_DB_URL -c "SELECT 1;" && break || sleep 2;
done
- name: Run tests
run: npm test
- name: Tear down DB
if: always()
run: railway down
The railway down step ensures the temporary instance is destroyed after the job, preventing orphaned resources.
Expected checks
railway upexits with status 0 and prints a line likePostgres service started.- The verification command
psql $RAILWAY_DB_URL -c "SELECT 1;"returns1without error. - In CI, the test job completes and the final
railway downstep runs, showingPostgres service stopped. - Logs from
railway logscontain no FATAL or PANIC messages for the Postgres service.
Recovery options (rollback)
Because railway up provisions a real database instance, you can roll back by running:
railway down
This stops and removes the temporary Postgres service, deleting any data created during the session. If you need to preserve data across sessions, avoid railway down and instead keep the service running; however, be aware of the free‑tier limits (50 MB storage, 10 M connections/month).
Limitations and practical verification
- The free tier caps storage at 50 MB and monthly connections at 10 M. Large test suites or bulk data loads may exceed these limits, causing the service to reject new connections. Monitor usage via the Railway dashboard.
- Credentials appear in plain text in the Railway UI; never commit
RAILWAY_DB_URLor the raw API key to source control. - If your application requires PostgreSQL extensions (e.g., PostGIS) that are not enabled by default, enable them through the Railway UI under the Postgres service’s “Settings” tab before running
railway up. - Network latency between the CI runner and the selected Railway region can add overhead to each query. Choose a region geographically close to your CI infrastructure (
railway config set region <name>). - Large schemas cause
railway upto pull more data on startup, increasing start‑up time. Consider using a minimal schema for local dev or seeding only necessary tables.
How to check that limits are not exceeded
After a test run, inspect the service metrics:
railway metrics
Look for “storage_used” and “total_connections”. If either approaches the free‑tier ceiling, either upgrade the plan or reduce data volume.
Summary
By adding Railway’s Postgres plugin, launching it with railway up, verifying via psql, and tearing down with railway down, you obtain a disposable, credential‑safe Postgres instance for both local development and CI pipelines. Keep an eye on storage and connection limits, enable any needed extensions via the UI, and always remove temporary instances to avoid orphaned resources.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.