Skip to content
Platform

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 强制 #

🌐 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.

通过管理 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/tokens
2
export SUPABASE_ACCESS_TOKEN="your-access-token"
3
export PROJECT_REF="your-project-ref"
4
5
# Get current SSL enforcement status
6
curl -X GET "https://api.supabase.com/v1/projects/$PROJECT_REF/ssl-enforcement" \
7
-H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN"
8
9
# Enable SSL enforcement
10
curl -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": true
16
}
17
}'
18
19
# Disable SSL enforcement
20
curl -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": false
26
}
27
}'

通过命令行管理 SSL 强制执行 #

🌐 Manage SSL enforcement via the CLI

开始吧:

🌐 To get started:

  1. 安装 Supabase CLI 1.37.0+。
  2. 登录到你的 Supabase 账户,使用 CLI。
  3. 确保你对正在启用 SSL 强制的项目拥有所有者或管理员权限

查看执行状态 #

🌐 Check enforcement status

你可以使用 CLI 的 get 子命令来检查 SSL 是否正在执行:

🌐 You can use the get subcommand of the CLI to check whether SSL is currently being enforced:

1
supabase ssl-enforcement get --project-ref {ref} --experimental

如果正在强制使用 SSL,请回应:

🌐 Response if SSL is being enforced:

1
SSL is being enforced.

如果没有强制使用 SSL,该如何响应:

🌐 Response if SSL is not being enforced:

1
SSL is *NOT* being enforced.

更新执行 #

🌐 Update enforcement

update 子命令用于更改你项目的 SSL 强制执行状态:

🌐 The update subcommand is used to change the SSL enforcement status for your project:

1
supabase ssl-enforcement update --project-ref {ref} --enable-db-ssl-enforcement --experimental

同样,要禁用 SSL 强制执行:

🌐 Similarly, to disable SSL enforcement:

1
supabase 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.

1
cat {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:

1
psql "postgresql://aws-0-eu-central-1.pooler.supabase.com:6543/postgres?sslmode=verify-full" -U postgres.<user>