Postgres SSL 强制
你的 Supabase 项目支持在未启用 SSL 的情况下连接到 Postgres 数据库,以最大化客户端兼容性。为了增强安全性,你可以阻止未使用 SSL 的客户端连接。
🌐 Your Supabase project supports connecting to the Postgres DB without SSL enabled to maximize client compatibility. For increased security, you can prevent clients from connecting if they're not using SSL.
禁用 SSL 强制只适用于连接 Postgres、Supavisor(共享连接池)和 PgBouncer(专用连接池);Supabase 提供的所有 HTTP API(例如 PostgREST、Storage、Auth)都会自动对所有传入连接强制使用 SSL。
🌐 Disabling SSL enforcement only applies to connections to Postgres, Supavisor (shared Connection Pooler) and PgBouncer (dedicated Connection Pooler); all HTTP APIs offered by Supabase (e.g., PostgREST, Storage, Auth) automatically enforce SSL on all incoming connections.
应用或更新 SSL 强制会触发数据库快速重启。在小型项目中,这通常只需几秒钟,但较大的数据库可能会中断更久。
🌐 Applying or updating SSL enforcement triggers a fast database reboot. On small projects this usually completes in a few seconds, but larger databases may see a longer interruption.
通过仪表板管理 SSL 强制 #
🌐 Manage SSL enforcement via the dashboard
可以通过仪表板数据库设置页面中的 SSL 配置部分下的“对传入连接强制使用 SSL”设置来配置 SSL 强制。
🌐 SSL enforcement can be configured via the "Enforce SSL on incoming connections" setting under the SSL Configuration section in Database Settings page of the dashboard.
更新 SSL 强制执行需要短暂重启数据库。这只是重启数据库, downtime 只有几分钟。
🌐 Updating SSL enforcement requires a brief database reboot. This restarts only the database and involves a few minutes of downtime.
通过管理 API 管理 SSL 强制执行 #
🌐 Manage SSL enforcement via the Management API
你也可以用管理 API 来管理 SSL 强制执行:
🌐 You can also manage SSL enforcement using the Management API:
1# Get your access token from https://supabase.com/dashboard/account/tokens2export SUPABASE_ACCESS_TOKEN="your-access-token"3export PROJECT_REF="your-project-ref"45# Get current SSL enforcement status6curl -X GET "https://api.supabase.com/v1/projects/$PROJECT_REF/ssl-enforcement" \7 -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN"89# Enable SSL enforcement10curl -X PUT "https://api.supabase.com/v1/projects/$PROJECT_REF/ssl-enforcement" \11 -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" \12 -H "Content-Type: application/json" \13 -d '{14 "requestedConfig": {15 "database": true16 }17 }'1819# Disable SSL enforcement20curl -X PUT "https://api.supabase.com/v1/projects/$PROJECT_REF/ssl-enforcement" \21 -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" \22 -H "Content-Type: application/json" \23 -d '{24 "requestedConfig": {25 "database": false26 }27 }'通过命令行管理 SSL 强制执行 #
🌐 Manage SSL enforcement via the CLI
开始吧:
🌐 To get started:
查看执行状态 #
🌐 Check enforcement status
你可以使用 CLI 的 get 子命令来检查 SSL 是否正在执行:
🌐 You can use the get subcommand of the CLI to check whether SSL is currently being enforced:
1supabase ssl-enforcement get --project-ref {ref} --experimental如果正在强制使用 SSL,请回应:
🌐 Response if SSL is being enforced:
1SSL is being enforced.如果没有强制使用 SSL,该如何响应:
🌐 Response if SSL is not being enforced:
1SSL is *NOT* being enforced.更新执行 #
🌐 Update enforcement
update 子命令用于更改你项目的 SSL 强制执行状态:
🌐 The update subcommand is used to change the SSL enforcement status for your project:
1supabase ssl-enforcement update --project-ref {ref} --enable-db-ssl-enforcement --experimental同样,要禁用 SSL 强制执行:
🌐 Similarly, to disable SSL enforcement:
1supabase ssl-enforcement update --project-ref {ref} --disable-db-ssl-enforcement --experimental关于 Postgres SSL 模式的小提示 #
🌐 A note about Postgres SSL modes
Postgres 在客户端支持多种 SSL 模式。这些模式提供不同等级的保护。根据你的需求,确保所使用的 SSL 模式能够执行所需的 SSL 连接强制和验证是很重要的。
🌐 Postgres supports multiple SSL modes on the client side. These modes provide different levels of protection. Depending on your needs, it is important to verify that the SSL mode in use is performing the required level of enforcement and verification of SSL connections.
| SSL 模式 | 加密 | 验证 CA | 验证主机名 | 描述 |
|---|---|---|---|---|
disable | 否 | 否 | 否 | 未使用 SSL。所有数据以明文传输。 |
allow | 可选 | 否 | 否 | 先尝试非 SSL 连接;如果服务器需要则回退到 SSL。 |
prefer | 可选 | 否 | 否 | 先尝试 SSL 连接;如果服务器不支持则回退到非 SSL。这是默认设置。 |
require | 是 | 否 | 否 | 始终使用 SSL,但不验证服务器证书或主机名。 |
verify-ca | 是 | 是 | 否 | 使用 SSL,并验证服务器证书是否由受信任的 CA 签发。 |
verify-full | 是 | 是 | 是 | 使用 SSL,验证 CA 证书,并确认主机名与证书匹配。在启用 SSL 强制时推荐使用。 |
Postgres 提供的最强模式是 verify-full,当启用 SSL 强制时,你很可能会想使用这个模式。要使用 verify-full,你需要下载你数据库的 Supabase CA 证书。该证书可以通过仪表板在 数据库设置页面 的 SSL 配置部分找到。
🌐 The strongest mode offered by Postgres is verify-full and this is the mode you most likely want to use when SSL enforcement is enabled. To use verify-full you will need to download the Supabase CA certificate for your database. The certificate is available through the dashboard under the SSL Configuration section in the Database Settings page.
一旦下载了CA证书,把它添加到Postgres使用的证书授权列表中。
🌐 Once the CA certificate has been downloaded, add it to the certificate authority list used by Postgres.
1cat {location of downloaded prod-ca-2021.crt} >> ~/.postgres/root.crt将 CA 证书添加到受信任的证书颁发机构列表后,使用 psql 或你的客户端库连接到 Supabase:
🌐 With the CA certificate added to the trusted certificate authorities list, use psql or your client library to connect to Supabase:
1psql "postgresql://aws-0-eu-central-1.pooler.supabase.com:6543/postgres?sslmode=verify-full" -U postgres.<user>