name: mg-railway-db-backup description: Backup a PostgreSQL database from a Railway environment. Use when the user asks to "backup db", "dump database", "save db from railway", "backup railway database", or "export db".
Railway DB Backup
Backup a PostgreSQL database from a Railway environment to a local file.
Inputs
Ask the user if not provided:
- project — Railway project name
- environment — Railway environment name to backup from (e.g.,
demo,production)
Procedure
Link to the correct project and environment using Railway MCP
link-environmenttool:- workspacePath:
- environmentName:
- workspacePath:
Ensure output directory exists:
mkdir -p <project-root>/.claude/localRun pg_dump — credentials piped from Railway CLI (never write credential values in commands):
VARS=$(railway variables -s Postgres -e <environment> --kv 2>/dev/null) && \ export PGPASSWORD=$(echo "$VARS" | grep '^PGPASSWORD=' | cut -d= -f2-) && \ PGUSER=$(echo "$VARS" | grep '^PGUSER=' | cut -d= -f2-) && \ PGDATABASE=$(echo "$VARS" | grep '^PGDATABASE=' | cut -d= -f2-) && \ DB_URL=$(echo "$VARS" | grep '^DATABASE_PUBLIC_URL=' | cut -d= -f2-) && \ PGHOST=$(echo "$DB_URL" | sed 's|postgresql://[^@]*@||;s|:.*||') && \ PGPORT=$(echo "$DB_URL" | sed 's|.*:||;s|/.*||') && \ pg_dump -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" \ --no-owner --no-acl -F c \ | gzip > <project-root>/.claude/local/<project>-<environment>-backup-$(date +%Y%m%d-%H%M%S).dump.gzThis reads credentials from
railway variablesinto shell vars at runtime — no secrets ever appear in the command text.Verify the backup file exists and report path + size to the user.
Notes
- Railway runs PostgreSQL 17 — if local pg_dump version is too old, use the one from
postgresql@17homebrew package - Backup format is custom (
-F c) for efficient restore with pg_restore --no-owner --no-aclstrips ownership/permissions for portable restores- Backups are stored in
.claude/local/which is git-ignored