railway-database by davila7/claude-code-templates
npx skills add https://github.com/davila7/claude-code-templates --skill railway-database添加官方的 Railway 数据库服务。这些是经过维护的模板,具有预配置的卷、网络和连接变量。
对于非数据库模板,请参阅 railway-templates 技能。
在创建之前,务必先检查是否存在现有数据库。
用户提及数据库
│
首先检查现有数据库
(查询环境配置中的 source.image)
│
┌────┴────┐
存在 不存在
│ │
│ 创建数据库
│ (CLI 或 API)
│ │
│ 等待部署完成
│ │
└─────┬─────┘
│
用户想要连接
服务吗?
│
┌─────┴─────┐
是 否
│ │
通过环境技能 完成 +
连接变量 建议连接
在创建数据库之前,请检查是否已存在数据库。
有关完整的环境配置结构,请参阅 environment-config.md。
railway status --json
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
然后查询环境配置并检查每个服务的 source.image:
query environmentConfig($environmentId: String!) {
environment(id: $environmentId) {
config(decryptVariables: false)
}
}
config.services 对象包含每个服务的配置。检查 source.image 是否为:
ghcr.io/railway/postgres* 或 postgres:* → Postgresghcr.io/railway/redis* 或 redis:* → Redisghcr.io/railway/mysql* 或 mysql:* → MySQLghcr.io/railway/mongo* 或 mongo:* → MongoDB| 数据库 | 模板代码 |
|---|---|
| PostgreSQL | postgres |
| Redis | redis |
| MySQL | mysql |
| MongoDB | mongodb |
获取项目上下文:
railway status --json
提取:
id - 项目 IDenvironments.edges[0].node.id - 环境 ID获取工作区 ID(不在状态输出中):
bash <<'SCRIPT'
${CLAUDE_PLUGIN_ROOT}/skills/lib/railway-api.sh \
'query getWorkspace($projectId: String!) {
project(id: $projectId) { workspaceId }
}' \
'{"projectId": "PROJECT_ID"}'
SCRIPT
bash <<'SCRIPT'
${CLAUDE_PLUGIN_ROOT}/skills/lib/railway-api.sh \
'query template($code: String!) {
template(code: $code) {
id
name
serializedConfig
}
}' \
'{"code": "postgres"}'
SCRIPT
这将返回部署所需的模板 id 和 serializedConfig。
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
重要提示: serializedConfig 是模板查询中的确切对象,而不是字符串。
部署后,其他服务使用引用变量进行连接。
有关完整的变量引用语法和连接模式,请参阅 variables.md。
使用私有/内部 URL 进行服务器间通信:
| 数据库 | 变量引用 |
|---|---|
| PostgreSQL | ${{Postgres.DATABASE_URL}} |
| Redis | ${{Redis.REDIS_URL}} |
| MySQL | ${{MySQL.MYSQL_URL}} |
| MongoDB | ${{MongoDB.MONGO_URL}} |
重要提示: 前端在用户的浏览器中运行,无法访问 Railway 的私有网络。它们必须使用公共 URL 或通过后端 API。
对于从前端直接访问数据库(不推荐):
${{MongoDB.MONGO_PUBLIC_URL}})更好的模式:前端 → 后端 API → 数据库
bash <<'SCRIPT'
# 1. 获取上下文
railway status --json
# 提取 project.id 和 environment.id
# 2. 获取工作区 ID
${CLAUDE_PLUGIN_ROOT}/skills/lib/railway-api.sh \
'query { project(id: "proj-id") { workspaceId } }' '{}'
# 3. 获取 Postgres 模板
${CLAUDE_PLUGIN_ROOT}/skills/lib/railway-api.sh \
'query { template(code: "postgres") { id serializedConfig } }' '{}'
# 4. 部署模板
${CLAUDE_PLUGIN_ROOT}/skills/lib/railway-api.sh \
'mutation deploy($input: TemplateDeployV2Input!) {
templateDeployV2(input: $input) { projectId workflowId }
}' \
'{"input": {"templateId": "...", "serializedConfig": {...}, "projectId": "...", "environmentId": "...", "workspaceId": "..."}}'
SCRIPT
使用 railway-environment 技能添加变量引用:
{
"services": {
"<backend-service-id>": {
"variables": {
"DATABASE_URL": { "value": "${{Postgres.DATABASE_URL}}" }
}
}
}
}
成功部署返回:
{
"data": {
"templateDeployV2": {
"projectId": "e63baedb-e308-49e9-8c06-c25336f861c7",
"workflowId": "deployTemplate/project/e63baedb-e308-49e9-8c06-c25336f861c7/xxx"
}
}
}
每个数据库模板创建:
| 错误 | 原因 | 解决方案 |
|---|---|---|
| 模板未找到 | 模板代码无效 | 使用:postgres、redis、mysql、mongodb |
| 权限被拒绝 | 用户缺乏访问权限 | 需要 DEVELOPER 角色或更高 |
| 项目未找到 | 项目 ID 无效 | 运行 railway status --json 获取正确的 ID |
railway-environment 技能暂存:DATABASE_URL: { "value": "${{Postgres.DATABASE_URL}}" }DATABASE_URL=${{Postgres.DATABASE_URL}}"railway-environment 技能添加变量引用railway-service 技能railway-deployment 技能每周安装次数
141
仓库
GitHub 星标数
22.6K
首次出现
2026年1月21日
安全审计
安装于
claude-code118
opencode108
gemini-cli105
cursor99
antigravity92
codex92
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.
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
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* or postgres:* → Postgresghcr.io/railway/redis* or redis:* → Redisghcr.io/railway/mysql* or mysql:* → MySQLghcr.io/railway/mongo* or mongo:* → MongoDB| Database | Template Code |
|---|---|
| PostgreSQL | postgres |
| Redis | redis |
| MySQL | mysql |
| MongoDB | mongodb |
Get project context:
railway status --json
Extract:
id - project IDenvironments.edges[0].node.id - environment IDGet 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
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.
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.
After deployment, other services connect using reference variables.
For complete variable reference syntax and wiring patterns, see variables.md.
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}} |
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):
${{MongoDB.MONGO_PUBLIC_URL}})Better pattern: Frontend → Backend API → Database
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
Use railway-environment skill to add the variable reference:
{
"services": {
"<backend-service-id>": {
"variables": {
"DATABASE_URL": { "value": "${{Postgres.DATABASE_URL}}" }
}
}
}
}
Successful deployment returns:
{
"data": {
"templateDeployV2": {
"projectId": "e63baedb-e308-49e9-8c06-c25336f861c7",
"workflowId": "deployTemplate/project/e63baedb-e308-49e9-8c06-c25336f861c7/xxx"
}
}
}
Each database template creates:
| 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 |
railway-environment skill to stage: DATABASE_URL: { "value": "${{Postgres.DATABASE_URL}}" }DATABASE_URL=${{Postgres.DATABASE_URL}}"railway-environment skill to add variable referencesrailway-service skillrailway-deployment skillWeekly Installs
141
Repository
GitHub Stars
22.6K
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code118
opencode108
gemini-cli105
cursor99
antigravity92
codex92