railway-database
关于
This skill adds official Railway database services (Postgres, Redis, MySQL, MongoDB) with pre-configured volumes and connection variables. Use it when developers request to add, connect, or wire up databases in their Railway projects. It specifically handles database services while directing other templates to the separate railway-templates skill.
快速安装
Claude Code
推荐/plugin add https://github.com/davila7/claude-code-templatesgit clone https://github.com/davila7/claude-code-templates.git ~/.claude/skills/railway-database在 Claude Code 中复制并粘贴此命令以安装该技能
技能文档
Railway Database
Add official Railway database services. These are maintained templates with pre-configured volumes, networking, and connection variables.
For non-database templates, see the railway-templates skill.
When to Use
- User asks to "add a database", "add Postgres", "add Redis", etc.
- User needs a database for their application
- User asks about connecting to a database
- User says "add postgres and connect to my server"
- User says "wire up the database"
Decision Flow
ALWAYS check for existing databases FIRST before creating.
User mentions database
│
Check existing DBs first
(query env config for source.image)
│
┌────┴────┐
Exists Doesn't exist
│ │
│ Create database
│ (CLI or API)
│ │
│ Wait for deployment
│ │
└─────┬─────┘
│
User wants to
connect service?
│
┌─────┴─────┐
Yes No
│ │
Wire vars Done +
via env suggest wiring
skill
Check for Existing Databases
Before creating a database, check if one already exists.
For full environment config structure, see environment-config.md.
railway status --json
Then query environment config and check source.image for each service:
query environmentConfig($environmentId: String!) {
environment(id: $environmentId) {
config(decryptVariables: false)
}
}
The config.services object contains each service's configuration. Check source.image for:
ghcr.io/railway/postgres*orpostgres:*→ Postgresghcr.io/railway/redis*orredis:*→ Redisghcr.io/railway/mysql*ormysql:*→ MySQLghcr.io/railway/mongo*ormongo:*→ MongoDB
Available Databases
| Database | Template Code |
|---|---|
| PostgreSQL | postgres |
| Redis | redis |
| MySQL | mysql |
| MongoDB | mongodb |
Prerequisites
Get project context:
railway status --json
Extract:
id- project IDenvironments.edges[0].node.id- environment ID
Get workspace ID (not in status output):
bash <<'SCRIPT'
${CLAUDE_PLUGIN_ROOT}/skills/lib/railway-api.sh \
'query getWorkspace($projectId: String!) {
project(id: $projectId) { workspaceId }
}' \
'{"projectId": "PROJECT_ID"}'
SCRIPT
Adding a Database
Step 1: Fetch Template
bash <<'SCRIPT'
${CLAUDE_PLUGIN_ROOT}/skills/lib/railway-api.sh \
'query template($code: String!) {
template(code: $code) {
id
name
serializedConfig
}
}' \
'{"code": "postgres"}'
SCRIPT
This returns the template's id and serializedConfig needed for deployment.
Step 2: Deploy Template
bash <<'SCRIPT'
${CLAUDE_PLUGIN_ROOT}/skills/lib/railway-api.sh \
'mutation deployTemplate($input: TemplateDeployV2Input!) {
templateDeployV2(input: $input) {
projectId
workflowId
}
}' \
'{
"input": {
"templateId": "TEMPLATE_ID",
"serializedConfig": SERIALIZED_CONFIG,
"projectId": "PROJECT_ID",
"environmentId": "ENVIRONMENT_ID",
"workspaceId": "WORKSPACE_ID"
}
}'
SCRIPT
Important: serializedConfig is the exact object from the template query, not a string.
Connecting to the Database
After deployment, other services connect using reference variables.
For complete variable reference syntax and wiring patterns, see variables.md.
Backend Services (Server-side)
Use the private/internal URL for server-to-server communication:
| Database | Variable Reference |
|---|---|
| PostgreSQL | ${{Postgres.DATABASE_URL}} |
| Redis | ${{Redis.REDIS_URL}} |
| MySQL | ${{MySQL.MYSQL_URL}} |
| MongoDB | ${{MongoDB.MONGO_URL}} |
Frontend Applications
Important: Frontends run in the user's browser and cannot access Railway's private network. They must use public URLs or go through a backend API.
For direct database access from frontend (not recommended):
- Use the public URL variables (e.g.,
${{MongoDB.MONGO_PUBLIC_URL}}) - Requires TCP proxy to be enabled
Better pattern: Frontend → Backend API → Database
Example: Add PostgreSQL
bash <<'SCRIPT'
# 1. Get context
railway status --json
# Extract project.id and environment.id
# 2. Get workspace ID
${CLAUDE_PLUGIN_ROOT}/skills/lib/railway-api.sh \
'query { project(id: "proj-id") { workspaceId } }' '{}'
# 3. Fetch Postgres template
${CLAUDE_PLUGIN_ROOT}/skills/lib/railway-api.sh \
'query { template(code: "postgres") { id serializedConfig } }' '{}'
# 4. Deploy template
${CLAUDE_PLUGIN_ROOT}/skills/lib/railway-api.sh \
'mutation deploy($input: TemplateDeployV2Input!) {
templateDeployV2(input: $input) { projectId workflowId }
}' \
'{"input": {"templateId": "...", "serializedConfig": {...}, "projectId": "...", "environmentId": "...", "workspaceId": "..."}}'
SCRIPT
Then Connect From Another Service
Use railway-environment skill to add the variable reference:
{
"services": {
"<backend-service-id>": {
"variables": {
"DATABASE_URL": { "value": "${{Postgres.DATABASE_URL}}" }
}
}
}
}
Response
Successful deployment returns:
{
"data": {
"templateDeployV2": {
"projectId": "e63baedb-e308-49e9-8c06-c25336f861c7",
"workflowId": "deployTemplate/project/e63baedb-e308-49e9-8c06-c25336f861c7/xxx"
}
}
}
What Gets Created
Each database template creates:
- A service with the database image
- A volume for data persistence
- Environment variables for connection strings
- TCP proxy for external access (where applicable)
Error Handling
| Error | Cause | Solution |
|---|---|---|
| Template not found | Invalid template code | Use: postgres, redis, mysql, mongodb |
| Permission denied | User lacks access | Need DEVELOPER role or higher |
| Project not found | Invalid project ID | Run railway status --json for correct ID |
Example Workflows
"add postgres and connect to the server"
- Check existing DBs via env config query
- If postgres exists: Skip to step 5
- If not exists: Deploy postgres template (fetch template → deploy)
- Wait for deployment to complete
- Identify target service (ask if multiple, or use linked service)
- Use
railway-environmentskill to stage:DATABASE_URL: { "value": "${{Postgres.DATABASE_URL}}" } - Apply changes
"add postgres"
- Check existing DBs via env config query
- If exists: "Postgres already exists in this project"
- If not exists: Deploy postgres template
- Inform user: "Postgres created. Connect a service with:
DATABASE_URL=${{Postgres.DATABASE_URL}}"
"connect the server to redis"
- Check existing DBs via env config query
- If redis exists: Wire up REDIS_URL via environment skill → apply
- If no redis: Ask "No Redis found. Create one?"
- Deploy redis template
- Wire REDIS_URL → apply
Composability
- Connect services: Use
railway-environmentskill to add variable references - View database service: Use
railway-serviceskill - Check logs: Use
railway-deploymentskill
GitHub 仓库
相关推荐技能
railway-new
元该Skill用于在Railway平台创建项目和部署服务,支持从零开始初始化项目或在现有项目中添加新服务。它能根据用户指令自动判断执行新项目创建或服务部署,并处理GitHub仓库连接等配置。特别适合需要快速在Railway部署应用或管理多服务项目的开发者。
railway-deployment
元这个Claude Skill用于管理Railway部署的完整生命周期,包括查看日志、重新部署、重启或移除部署。它特别适合部署可见性(列表、状态、历史记录)和故障排除(日志、错误、故障排查)。注意:此技能仅用于部署管理,要完全删除服务需使用railway-environment技能。
railway-status
元该Skill用于检查当前目录下Railway项目的实时状态,包括部署状态、运行情况和可用性。当开发者询问"railway status"、"is it running"或"what's deployed"等部署状态问题时自动触发。它通过railway-cli获取项目信息,但配置查询需使用专门的railway-environment skill处理。
railway-domain
元该Skill用于管理Railway服务的域名,支持添加、查看和移除操作。开发者可以通过它生成Railway提供的域名、添加自定义域名,或查询服务的当前URL。它基于railway-cli工具,适用于需要快速管理服务域名的日常开发场景。
