Using SQLAlchemy with Supabase
部署到自动扩展服务器: #
🌐 Deploying to auto-scaling servers:
如果你要部署到:
🌐 If you are deploying to:
- 边缘函数
- 无服务器函数
- 水平自动扩展部署
建议你通过事务模式(端口 6543)连接到连接池,可以在仪表板上点击 Connect 找到它。
🌐 It is recommended that you connect with the pooler in transaction mode (port 6543), which can be found on the dashboard by clicking Connect.
1# Example transaction mode string:2postgres://[db-user].[project-ref]:[db-password]@aws-0-[aws-region].pooler.supabase.com:6543在使用事务模式时,你应该使用 NullPool 设置:
🌐 When using transaction mode, you should use the NullPool setting:
1from sqlalchemy.pool import NullPool23con = sqlalchemy.create_engine(url, client_encoding='utf8', poolclass=NullPool)在依赖 Supavisor 时,选择合适的池大小很重要。这份指南可以带你了解整个过程:
🌐 When relying on Supavisor, it's important to pick an adequate pool size. This guide can walk you through the process:
部署到固定服务器 #
🌐 Deploying to stationary servers
对于固定服务器,例如虚拟机和长期运行的容器,建议使用你的直接连接字符串,可以在仪表板上点击 Connect 找到。
🌐 For stationary servers, such as VMs and long-running containers, it is recommended to use your direct connection string, which can be found on the dashboard by clicking Connect.
1# Example DB string:2postgresql://postgres:[PASSWORD]@db.[PROJECT REF].supabase.co:5432/postgres这个连接映射到一个 IPv6 地址,不能在 IPv4 环境下使用。
🌐 The connection maps to an IPv6 address, and cannot operate in an IPv4 environment.
正在检查 IPv6 支持: #
🌐 Checking IPv6 support:
大多数服务都兼容IPv6。不过,有一些主要服务只接受IPv4连接:
🌐 The majority of services are IPv6 compatible. However, there are a few prominent services that only accept IPv4 connections:
如果你还不确定你的网络是否支持 IPv6,你可以在部署服务器上运行这个 cURL 命令:
🌐 If you're still unsure if your network supports IPv6, you can run this cURL command on your deployment server:
1curl -6 https://ifconfig.co/ip如果命令返回一个IPv6地址,说明网络支持IPv6。
🌐 If the command returns an IPv6 address, the network is IPv6 compatible.
如果你的部署环境不兼容 IPv6,那么可以考虑:
🌐 If your deployment environment is not IPv6 compatible, then consider:
- 在会话中使用 Supavisor 连接池(端口 5432)
- 如果你使用的是专业版或更高级的计划,可以启用IPv4 插件
选择内部池大小 #
🌐 Choosing an internal pool size
关键池设置:
- pool_size:这设置了连接池中永久连接的最大数量。SQLAlchemy 会根据需要创建连接,直到达到这个限制。 max_overflow:允许在 pool_size 基础上为临时需求高峰创建额外连接。这些临时连接在使用后会关闭。
1# Example configurations2engine = create_engine(3 "postgresql+psycopg2://me@localhost/mydb", pool_size=20, max_overflow=154)一般来说,如果你使用 Supabase 数据库 REST 客户端,尽量将部署使用的连接限制在可用连接的 40% 左右。否则,你可以谨慎地将使用量增加到大约 80%。这些百分比是灵活的,取决于你应用的使用情况和设置。监控连接使用情况,以确定最佳分配,同时不剥夺其他服务器所需的连接。
🌐 As a rule of thumb, if you're using the Supabase Database REST Client, try to limit the connections used by your deployment to 40% of available connections. Otherwise, you can cautiously increase usage to around 80%. These percentages are flexible and depend on your application's usage and setup. Monitor connection usage to determine the optimal allocation without depriving other servers of necessary connections.
如何监控实时连接 #
🌐 How to monitor live connections
可以通过 Supabase Grafana 仪表板监控连接使用情况。它可以实时查看超过 200 个数据库指标,比如 CPU、EBS 以及活跃的直接/连接池连接的图表。这对于监控和调试实例非常有用。
🌐 Connection usage can be monitored with a Supabase Grafana Dashboard. It provides realtime visibility of over 200 database metrics, such as graphs of CPU, EBS, and active direct/pooler connections. It can be extremely useful for monitoring and debugging instances.
你可以查看我们的GitHub 仓库以获取本地部署或在Fly.io上的免费云部署的设置说明。想要了解连接监控的完整讲解,你可以查看这个指南
🌐 You can check our GitHub repo for setup instructions for local deployments or free cloud deployments on Fly.io. For a complete explainer on connection monitoring, you can check out this guide