Prisma Error Management
本指南已不再使用。请使用 Supabase 文档 中的故障排除指南。
处理特定错误 #
🌐 Addressing specific errors
Prisma 和其他库不同,它使用 查询参数来进行配置。
🌐 Prisma, unlike other libraries, uses query parameters for configurations.
有些可以用来处理特定错误,并且可以像这样附加到你的连接字符串的末尾:
🌐 Some can be used to address specific errors and can be appended to end of your connection string like so:
1.../postgres?KEY1=VALUE&KEY2=VALUE&KEY3=VALUE无法连接数据库服务器 #
🌐 Can't reach database server
把 connect_timeout 增加到 30 秒,并检查确保你使用的是有效的连接字符串。
🌐 Increase connect_timeout to 30s and check to make sure you are using a valid connection string.
1.../postgres?connect_timeout=30从连接池获取新连接超时了 #
🌐 Timed out fetching a new connection from the connection pool
把 pool_timeout 增加到 30 秒。
🌐 Increase pool_timeout to 30s .
1.../postgres?pool_timeout=30预处理语句已存在 #
🌐 Prepared statement already exists
在连接字符串中加入 pgbouncer=true。
🌐 Add pgbouncer=true to the connection string.
1.../postgres?pgbouncer=true已达到最大客户端连接数 #
🌐 Max client connections reached
看看这份指南来处理这个错误
🌐 Check out this guide for managing this error
服务器已关闭连接 #
🌐 Server has closed the connection
根据这个 Prisma 的 GitHub 问题,这可能与查询返回的大量数据有关。对于特别大的请求,尝试限制返回的总行数。
🌐 According to this GitHub Issue for Prisma, it may be related to large return values for queries. Try to limit the total amount of rows returned for particularly large requests.
检测到漂移:你的数据库模式与迁移历史不同步 #
🌐 Drift detected: Your database schema is not in sync with your migration history
Prisma 会尝试作为你数据库结构的真实来源。如果你在 Prisma 迁移之外对某些数据库对象 CREATE、DROP 或 ALTER 进行了操作,它很可能会检测到偏移,并可能会通过清理你的模式来提供修复方案。为避免这个问题,可以尝试基线化你的迁移。
🌐 Prisma will try to act as the source of truth for your database structures. If you CREATE, DROP, or ALTER database objects outside of a Prisma Migration, it is likely to detect drift and may offer to correct the situation by purging your schemas. To circumvent this issue, try baselining your migrations.
有些用户在GitHub 讨论中讨论了他们是如何处理这个问题的。
🌐 Some users have discussed how they managed this problem in a GitHub Discussion.
管理建议 #
🌐 Management suggestions
为 Prisma 创建一个自定义角色来提高可观察性 #
🌐 Make a custom role for Prisma to increase observability
把你的数据库想象成一所房子,而用户就是拿着钥匙的人。
- 默认情况下,大多数开发者使用“主密钥”(
postgres角色)来访问所有内容。但给 Prisma 自己的密钥更安全!这样,它只能访问它需要的房间(表格)。 - 通常给 Prisma 自己的密钥会更安全!这样,它只能访问它需要的房间(表格)。
- 另外,有了单独的密钥,通过像 PGAudit 和 pg_stat_activity 这样的监控工具,更容易看到 Prisma 在你家里做了什么。
创建 Prisma 用户 #
🌐 Creating the Prisma user
1create user "prisma" with password 'secret_password' bypassrls createdb;Prisma 需要
createdb修饰符 来创建影子数据库。它用这些数据库来帮助管理迁移。
把新的用户的所有权给 Postgres #
🌐 Give Postgres ownership of the new user
这让你可以在 Dashboard 查看 Prisma 的迁移更改
🌐 This allows you to view Prisma migration changes in the Dashboard
1grant "prisma" to "postgres";保管好哦! #
🌐 Keep it safe!
为 Prisma 使用一个强密码。Bitwarden 提供一个免费的密码生成器,可以帮你生成一个。
🌐 Use a strong password for Prisma. Bitwarden provides a free password generator that can make one for you.
如果你之后需要更改,可以使用下面的 SQL:
🌐 If you need to change it later, you can use the below SQL:
1alter user "prisma" with password 'new_password';授予 Prisma 访问权限 #
🌐 Grant Prisma access
下面的例子授予 Prisma 对公共 schema 中所有数据库对象的完全权限:
🌐 The below example gives Prisma full authority over all database objects in the public schema:
1-- Grant it necessary permissions over the relevant schemas (public)2 grant usage on schema public to prisma;3 grant create on schema public to prisma;4 grant all on all tables in schema public to prisma;5 grant all on all routines in schema public to prisma;6 grant all on all sequences in schema public to prisma;7 alter default privileges for role postgres in schema public grant all on tables to prisma;8 alter default privileges for role postgres in schema public grant all on routines to prisma;9 alter default privileges for role postgres in schema public grant all on sequences to prisma;想了解更多关于访问权限的设置,可以看看这篇文章
优化 Prisma 查询 #
🌐 Optimize Prisma queries
在 查询性能顾问 中,你可以按角色查看执行时间长或访问频繁的查询:
🌐 In the Query Performance Advisor, you can view long-running or frequently accessed queries by role:
选择一个查询可以显示改进其性能的建议
🌐 Selecting a query can reveal suggestions to improve its performance
配置连接 #
🌐 Configuring connections
有用的链接:
🌐 Useful Links:
Supabase 提供 3 个数据库连接字符串,如果需要可以同时使用。你可以在仪表板上点击 Connect 找到它们。
🌐 Supabase provides 3 database connection strings that can be used simultaneously if necessary. You can find them on the dashboard by clicking Connect.
直接连接 #
🌐 Direct connection
最好用于固定服务器,比如虚拟机和长期运行的容器,但它只在 IPv6 环境下有效,除非启用了 IPv4 附加功能。如果你不确定你的网络是否支持 IPv6,点击这里查看。
🌐 Best used with stationary servers, such as VMs and long-standing containers, but it only works in IPv6 environments unless the IPv4 Add-On is enabled. If you are unsure if your network is IPv6 compatible, check here.
1# Example Connection23postgresql://postgres:[PASSWORD]@db.[PROJECT REF].supabase.co:5432/postgresSupavisor 正在会话模式(端口 5432) #
🌐 Supavisor in session mode (port 5432)
1# Example Connection23postgres://[DB-USER].[PROJECT REF]:[PASSWORD]@aws-0-[REGION].pooler.supabase.com:5432/postgres在仅限 IPv4 的环境中工作的情况下,直接连接的替代方案。
🌐 An alternative to direct connections when working in IPv4-only environments.
会话模式是迁移的一个不错的选择
交易模式下的Supavisor(端口6543) #
🌐 Supavisor in transaction mode (port 6543)
1# Example Connection23postgres://[DB-USER].[PROJECT REF]:[PASSWORD]@aws-0-[REGION].pooler.supabase.com:6543/postgres应该在部署到以下时使用:
🌐 Should be used when deploying to:
- 水平自动扩展服务器
- 边缘/无服务器部署
在无服务器/边缘环境中工作时,建议先设置 connection_limit=1,如果有必要再逐渐增加它。
🌐 When working in serverless/edge environments, it is recommended to set the connection_limit=1 and then gradually increase it if necessary.