Skip to main content

使用 TypeScript 和 Prisma Postgres 连接你的数据库

在 PDP 控制台中设置 Prisma Postgres 数据库

¥Set up a Prisma Postgres database in the PDP Console

按照以下步骤创建你的 Prisma Postgres 数据库:

¥Follow these steps to create your Prisma Postgres database:

  1. 登录到

    ¥Log in to .

  2. 在你选择的 workspace 中,单击新建项目按钮。

    ¥In a workspace of your choice, click the New project button.

  3. 在名称字段中为你的项目输入一个名称,例如 hello-ppg。

    ¥Type a name for your project in the Name field, e.g. hello-ppg.

  4. 在 Prisma Postgres 部分中,单击“开始”按钮。

    ¥In the Prisma Postgres section, click the Get started button.

  5. 在“区域”下拉菜单中,选择最接近你当前位置的区域,例如美国东部(弗吉尼亚北部)。

    ¥In the Region dropdown, select the region that's closest to your current location, e.g. US East (N. Virginia).

  6. 单击创建项目按钮。

    ¥Click the Create project button.

此时,你将被重定向到仪表板,你需要等待几秒钟,而数据库的状态将从 PROVISIONING 更改为 ACTIVATING 再更改为 CONNECTED

¥At this point, you'll be redirected to the Dashboard where you will need to wait for a few seconds while the status of your database changes from PROVISIONING, to ACTIVATING to CONNECTED.

出现绿色 CONNECTED 标签后,你的数据库即可使用。

¥Once the green CONNECTED label appears, your database is ready to use.

在控制台 UI 中,你将看到一个 .env 文件的代码片段,其中定义了两个环境变量。

¥In the Console UI, you'll see a code snippet for a .env file with two environment variables defined.

在本地项目中设置环境变量

¥Set environment variables in your local project

从控制台 UI 复制 DATABASE_URL 环境变量并将其粘贴到 .env 文件中。你的 .env 文件应该类似于此:

¥Copy the DATABASE_URL environment variable from the Console UI and paste it into your .env file. Your .env file should look similar to this:

.env
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=ey..."

通过在 .env 文件中设置 DATABASE_URL,你可以确保 Prisma ORM 可以连接到你的数据库。DATABASE_URL 用于 Prisma 模式中的 datasource 块:

¥By setting the DATABASE_URL in the .env file, you're ensuring that Prisma ORM can connect to your database. The DATABASE_URL is used in the datasource block in your Prisma schema:

prisma/schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

就是这样!你现在可以开始使用 Prisma CLI 与你的 Prisma Postgres 数据库交互。在下一节中,你将了解如何使用 Prisma CLI 创建和运行针对数据库的迁移以更新其架构。

¥That's it! You can now start using the Prisma CLI to interact with your Prisma Postgres database. In the next section, you'll learn how to use the Prisma CLI to create and run migrations against your database to update its schema.