database by railwayapp/railway-skills
npx skills add https://github.com/railwayapp/railway-skills --skill database添加官方的 Railway 数据库服务。这些是经过维护的模板,具有预配置的卷、网络和连接变量。
对于非数据库模板,请参阅 templates 技能。
在创建之前,务必先检查是否已存在数据库。
用户提及数据库
│
首先检查现有数据库
(查询环境配置中的 source.image)
│
┌────┴────┐
存在 不存在
│ │
│ 创建数据库
│ (CLI 或 API)
│ │
│ 等待部署完成
│ │
└─────┬─────┘
│
用户是否想要
连接服务?
│
┌─────┴─────┐
是 否
│ │
通过环境技能 完成 +
连接变量 建议连接
在创建数据库之前,请检查是否已存在。
有关完整的环境配置结构,请参阅 environment-config.md。
railway status --json
然后查询环境配置,并检查每个服务的 :
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
source.imagequery 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'
scripts/railway-api.sh \
'query getWorkspace($projectId: String!) {
project(id: $projectId) { workspaceId }
}' \
'{"projectId": "PROJECT_ID"}'
SCRIPT
bash <<'SCRIPT'
scripts/railway-api.sh \
'query template($code: String!) {
template(code: $code) {
id
name
serializedConfig
}
}' \
'{"code": "postgres"}'
SCRIPT
这将返回部署所需的模板 id 和 serializedConfig。
bash <<'SCRIPT'
scripts/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
scripts/railway-api.sh \
'query { project(id: "proj-id") { workspaceId } }' '{}'
# 3. 获取 Postgres 模板
scripts/railway-api.sh \
'query { template(code: "postgres") { id serializedConfig } }' '{}'
# 4. 部署模板
scripts/railway-api.sh \
'mutation deploy($input: TemplateDeployV2Input!) {
templateDeployV2(input: $input) { projectId workflowId }
}' \
'{"input": {"templateId": "...", "serializedConfig": {...}, "projectId": "...", "environmentId": "...", "workspaceId": "..."}}'
SCRIPT
使用 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 |
environment 技能暂存:DATABASE_URL: { "value": "${{Postgres.DATABASE_URL}}" }DATABASE_URL=${{Postgres.DATABASE_URL}}"environment 技能添加变量引用service 技能deployment 技能每周安装次数
872
仓库
GitHub 星标数
214
首次出现
2026年1月20日
安全审计
安装于
opencode734
codex718
gemini-cli680
github-copilot650
claude-code639
amp576
Add official Railway database services. These are maintained templates with pre-configured volumes, networking, and connection variables.
For non-database templates, see the 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'
scripts/railway-api.sh \
'query getWorkspace($projectId: String!) {
project(id: $projectId) { workspaceId }
}' \
'{"projectId": "PROJECT_ID"}'
SCRIPT
bash <<'SCRIPT'
scripts/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'
scripts/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
scripts/railway-api.sh \
'query { project(id: "proj-id") { workspaceId } }' '{}'
# 3. Fetch Postgres template
scripts/railway-api.sh \
'query { template(code: "postgres") { id serializedConfig } }' '{}'
# 4. Deploy template
scripts/railway-api.sh \
'mutation deploy($input: TemplateDeployV2Input!) {
templateDeployV2(input: $input) { projectId workflowId }
}' \
'{"input": {"templateId": "...", "serializedConfig": {...}, "projectId": "...", "environmentId": "...", "workspaceId": "..."}}'
SCRIPT
Use 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 |
environment skill to stage: DATABASE_URL: { "value": "${{Postgres.DATABASE_URL}}" }DATABASE_URL=${{Postgres.DATABASE_URL}}"environment skill to add variable referencesservice skilldeployment skillWeekly Installs
872
Repository
GitHub Stars
214
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubWarnSocketPassSnykPass
Installed on
opencode734
codex718
gemini-cli680
github-copilot650
claude-code639
amp576
Azure 升级评估与自动化工具 - 轻松迁移 Functions 计划、托管层级和 SKU
64,099 周安装