Skip to main content

部署到 Railway

本指南介绍如何将使用 Prisma ORM 和 Prisma Postgres 的应用部署到 Railway。该应用公开 REST API,并使用 Prisma 客户端查询 Prisma Postgres 数据库。你的应用将在 Railway 上运行并连接到托管的 Prisma Postgres 数据库。

¥This guide explains how to deploy an app that uses Prisma ORM and Prisma Postgres to Railway. The app exposes a REST API and uses Prisma Client to query a Prisma Postgres database. Your app will run on Railway and connect to a managed Prisma Postgres database.

Railway 是一个部署平台,它通过即时部署、内置可观察性和轻松扩展来简化软件开发生命周期。它支持来自流行注册表的代码存储库和容器镜像。Railway 负责配置管理、环境变量,并在服务之间提供私有网络。

¥Railway is a deployment platform that simplifies the software development lifecycle with instant deployments, built-in observability, and effortless scaling. It supports code repositories and container images from popular registries. Railway handles configuration management, environment variables, and provides private networking between services.

要获得预连接了 Prisma ORM、Prisma Postgres 和 Railway 的 Next.js 项目,请使用 Prisma Railway 官方模板

¥To get a pre-wired Next.js Project with Prisma ORM, Prisma Postgres, and Railway, use the Official Prisma Railway Template.

此模板可自动配置和设置 Prisma Postgres 数据库,并在部署时将其直接链接到 Next.js 应用,只需单击一下即可完成整个项目。

¥This template automates the provisioning and setup of a Prisma Postgres database, linking it directly to your Next.js application upon deployment, making the entire project ready with just one click.

先决条件

¥Prerequisites

要开始使用 Prisma,你只需要:

¥To get started, all you need is:

注意

如果你尚未准备好项目,可以使用我们的 Prisma 项目示例。这是一个使用 Prisma ORM 的简单 Hono 应用,包含一个 REST API、一个用于测试端点的前端以及一个已定义的带有迁移的 Prisma 模式。

¥If you don't have a project ready, you can use our example Prisma project. It's a simple Hono application that uses Prisma ORM and includes a REST API, a frontend for testing endpoints, and a defined Prisma schema with migrations.

部署你的应用

¥Deploy your application

1. 创建一个新的 Railway 项目

¥ Create a new Railway project

  1. 前往 Railway 仪表盘

    ¥Go to the Railway dashboard

  2. 点击“创建新项目”

    ¥Click Create a New Project

  3. 选择 GitHub 仓库

    ¥Select GitHub Repo

  4. 点击“配置 GitHub 应用”并授权 Railway

    ¥Click Configure GitHub App and authorize Railway

  5. 选择你的仓库

    ¥Select your repository

你的应用现在正在部署到 Railway,但如果没有数据库连接,它将无法正常运行。

¥Your application is now deploying to Railway, but it won't properly run without a database connection.

在下一部分中,你将在 Railway 中配置数据库并设置 DATABASE_URL 环境变量。

¥In the next section, you'll configure a database and set the DATABASE_URL environment variable in Railway.

Railway deploying application

配置你的数据库

¥Configure your database

1. 获取数据库连接字符串

¥ Get your database connection string

你需要一个 Prisma Postgres 连接字符串。获取注释的方法有两种:

¥You'll need a Prisma Postgres connection string. There are two ways to obtain one:

  • 上创建一个新数据库

    ¥Create a new database on

  • 为临时数据库运行 npx create-db(无需账户)

    ¥Run npx create-db for a temporary database (no account required)

2. 将数据库 URL 添加到 Railway

¥ Add the database URL to Railway

  1. 在你的 Railway 项目中,打开你的服务

    ¥In your Railway project, open your service

  2. 转到“变量”选项卡

    ¥Go to the Variables tab

  3. 点击“新建变量”

    ¥Click New Variable

  4. 将名称设置为 DATABASE_URL

    ¥Set the name to DATABASE_URL

  5. 粘贴你的数据库连接字符串作为值

    ¥Paste your database connection string as the value

  6. 点击“部署”以使用新的环境变量重新部署你的应用

    ¥Click Deploy to redeploy your application with the new environment variable

Railway environment variables setup

3. 访问你的应用

¥ Access your application

部署完成并配置数据库 URL 后:

¥Once the deployment completes with the database URL configured:

  1. 导航至“设置”选项卡

    ¥Navigate to the Settings tab

  2. 在“网络”下,点击“生成域”

    ¥Under Networking, click Generate Domain

  3. 你的应用将在生成的 URL 上可用。

    ¥Your application will be available at the generated URL

Railway networking settings

转到生成的 URL,你将看到已部署的应用!

¥Go to the generated URL and you'll see your deployed app!

如果你使用了示例项目,你应该会看到三个已设置的 API 端点:

¥If you used the example project, you should see three api endpoints already set up:

  • 检查 API 状态(/api)

    ¥Check API status (/api)

  • 加载 feed (/api/feed)

    ¥Load feed (/api/feed)

  • 种子数据 (/api/seed)

    ¥Seed data (/api/seed)

Railway deployed application

如果你看到任何错误:

¥If you see any errors:

  • 等待片刻并刷新

    ¥Wait a minute and refresh

  • 确保 DATABASE_URL 已设置

    ¥Ensure DATABASE_URL is set

  • 检查服务日志

    ¥Check the service logs

  • 重新部署

    ¥Redeploy

要了解更多关于 Railway 为你的应用提供的各种功能,请访问 Railway 文档

¥To learn more about the various features Railway offers for your application, visit the Railway documentation.