网络限制
本主题讲解了如何为你的 Supabase 项目的数据库配置网络限制。网络限制可以让你控制哪些 IP 范围可以连接到 Postgres 及其连接池,从而减少项目被未授权访问的风险。
🌐 This topic explains how to configure network restrictions for your Supabase project's database. Network restrictions let you control which IP ranges can connect to Postgres and its pooler, reducing your project's exposure to unauthorized access.
如果你在数据库设置中找不到网络限制部分,请在基础设施设置中更新你的Postgres版本。
🌐 If you can't find the Network Restrictions section in your Database Settings, update your Postgres version in Infrastructure Settings.
每个 Supabase 项目都支持对允许连接 Postgres 及其连接池的 IP 范围进行可配置的限制。这些限制会在流量到达你的数据库之前就被执行。即使没有 IP 限制的连接,也仍然需要使用有效的数据库凭据进行身份验证。
🌐 Each Supabase project supports configurable restrictions on the IP ranges allowed to connect to Postgres and its pooler. These restrictions are enforced before traffic reaches your database. Connections that aren't restricted by IP still need to authenticate with valid database credentials.
如果直接连接到你的数据库会【解析到 IPv6 地址】(/dashboard/project//database/settings),请同时将 IPv4 和 IPv6 的 CIDR 添加到你的允许列表中。网络限制适用于所有连接路径,无论是池化连接还是直接连接。不过有两个例外:如果你在 IPv6 迁移上有延期,或者你有【IPv4 附加组件】(/dashboard/project//settings/addons),只需要添加 IPv4 的 CIDR 就可以。
🌐 If direct connections to your database resolve to an IPv6 address, add both IPv4 and IPv6 CIDRs to your allowlist. Network restrictions apply to all connection routes, whether pooled or direct. There are two exceptions: if you have an extension on the IPv6 migration, or if you have the IPv4 add-on, you only need to add IPv4 CIDRs.
通过仪表板配置 #
🌐 Configure with the dashboard [#to-get-started-via-the-dashboard]
在仪表板上配置网络限制:
🌐 To configure network restrictions with the dashboard:
通过 CLI 配置 #
🌐 Configure with the CLI [#to-get-started-via-the-cli]
使用命令行接口配置网络限制:
🌐 To configure network restrictions with the CLI:
- 安装 Supabase CLI 1.22.0+。
- 登录到你的Supabase账户。
- 如果你的项目是在2022年12月23日之前创建的,请在使用网络限制之前将其升级到最新的 Supabase 版本。
- 确保你对该项目有拥有者或管理员权限。
查看限制 #
🌐 Check restrictions
查看你当前的网络限制:
🌐 To check your current network restrictions:
-
完成 使用 CLI 配置 中的步骤。
-
运行
get子命令以获取当前生效的限制:1> supabase network-restrictions get --project-ref {ref} --experimental2DB Allowed IPv4 CIDRs: &[183.12.1.1/24]3DB Allowed IPv6 CIDRs: &[2001:db8:3333:4444:5555:6666:7777:8888/64]4Restrictions applied successfully: true如果从未施加限制,允许的 CIDR 列表为空,
Restrictions applied successfully是false。所有 IP 都可以连接:1> supabase network-restrictions get --project-ref {ref} --experimental2DB Allowed IPv4 CIDRs: []3DB Allowed IPv6 CIDRs: []4Restrictions applied successfully: false
更新限制 #
🌐 Update restrictions
要更新你的网络限制:
🌐 To update your network restrictions:
-
完成 使用 CLI 配置 中的步骤。
-
使用你想允许的 CIDRs 运行
update子命令:1> supabase network-restrictions update --project-ref {ref} --db-allow-cidr 183.12.1.1/24 --db-allow-cidr 2001:db8:3333:4444:5555:6666:7777:8888/64 --experimental2DB Allowed IPv4 CIDRs: &[183.12.1.1/24]3DB Allowed IPv6 CIDRs: &[2001:db8:3333:4444:5555:6666:7777:8888/64]4Restrictions applied successfully: true你提供的 CIDR 会替换之前应用的所有限制。如果想保留现有的限制,请在
update命令中将它们和新的 CIDR 一起包含。
向现有限制中添加 CIDR #
🌐 Append a CIDR to existing restrictions
要在你现有的限制中添加一个 CIDR:
🌐 To append a CIDR to your existing restrictions:
-
完成 使用 CLI 配置 中的步骤。
-
运行
update子命令并加上--append标志来添加 CIDR,而不替换现有限制:1> supabase network-restrictions update --project-ref {ref} --db-allow-cidr 1.2.3.4/32 --append --experimental2DB Allowed IPv4 CIDRs: &[183.12.1.1/24 1.2.3.4/32]3DB Allowed IPv6 CIDRs: &[2001:db8:3333:4444:5555:6666:7777:8888/64]4Restrictions applied successfully: true
取消限制 #
🌐 Remove restrictions
要移除所有网络限制:
🌐 To remove all network restrictions:
-
完成 使用 CLI 配置 中的步骤。
-
运行
update子命令并使用 CIDR0.0.0.0/0来移除所有限制:1> supabase network-restrictions update --project-ref {ref} --db-allow-cidr 0.0.0.0/0 --db-allow-cidr ::/0 --experimental2DB Allowed IPv4 CIDRs: &[0.0.0.0/0]3DB Allowed IPv6 CIDRs: &[::/0]4Restrictions applied successfully: true
限制 #
🌐 Limitations
- 网络限制适用于 Postgres 和数据库连接池。但它们不适用于像 PostgREST、Storage 和 Auth 这样的 HTTPS API,也不适用于 Supabase 客户端库,比如 supabase-js。
- 在应用网络限制时,Edge 函数无法直接访问数据库。请改用 supabase-js 从 Edge 函数连接数据库。