apps/docs/content/troubleshooting/using-sqlalchemy-with-supabase-FUqebT.mdx
If you are deploying to:
It is recommended that you connect with the pooler in transaction mode (port 6543), which can be found on the dashboard by clicking Connect.
# Example transaction mode string:
postgres://[db-user].[project-ref]:[db-password]@aws-0-[aws-region].pooler.supabase.com:6543
When using transaction mode, you should use the NullPool setting:
from sqlalchemy.pool import NullPool
con = sqlalchemy.create_engine(url, client_encoding='utf8', poolclass=NullPool)
When relying on Supavisor, it's important to pick an adequate pool size. This guide can walk you through the process:
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.
# Example DB string:
postgresql://postgres:[PASSWORD]@db.[PROJECT REF].supabase.co:5432/postgres
The connection maps to an IPv6 address, and cannot operate in an IPv4 environment.
The majority of services are IPv6 compatible. However, there are a few prominent services that only accept IPv4 connections:
If you're still unsure if your network supports IPv6, you can run this cURL command on your deployment server:
curl -6 https://ifconfig.co/ip
If the command returns an IPv6 address, the network is IPv6 compatible.
If your deployment environment is not IPv6 compatible, then consider:
Key Pool Settings:
# Example configurations
engine = create_engine(
"postgresql+psycopg2://me@localhost/mydb", pool_size=20, max_overflow=15
)
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.
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.
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