Problem connecting Django project with Supabase Postgres DB

I have a Django project running smoothly, but when it needs to access the database specificaly in production (Vercel serverless function) it can’t connect:

connection to server at "db.XXXXXXX.supabase.co" (2600:1f18:2e13:9d0a:17a3:b94b:5d9:cf52), port 5432 failed: Cannot assign requested address
	Is the server running on that host and accepting TCP/IP connections?

I can connect to the database locally, it only occurs in Vercel. I quadra-checked the environment variables.

Code is here, i reproduced the error in this public project to facilitate. Anyone can do it creating both free tier Vercel + Supabase project and DB, respectively.

If you are willing to run it, read the README, specifically about the environment variables.

The root url view tries to connect programmatically via psycopg2 using the DB_PASSWORD variable, and the DATABASE_URL error only occurs if you try to make any request that would use the database, for exampe log in (even with wrong credentials (duh))

The project uses Django (Vercel-hosted) + Postgres (Supabase-hosted).

I don’t usually post in forums, if you have something i could improve in this post feel free to comment.

Depending on if your use case allows for it, try connecting via transaction pooler method provided by Supabase. It is IPv4 compatible and most suitable for serverless functions.

I’m experiencing the exact same issue! Did you manage to find a solution yet?

Follow:

  • Open your database in Supabase
  • Click “connect“ (topbar)
  • In the default tab (Connection String) you will see 3 types of connection, skip Direct Connection (it is not ipv4 compatible, which we need to connect with Vercel) and test with Transaction and Session poolers.

I solved doing this, but for some reason i can’t use session pooler.

1 Like

Thank you! I followed your instructions and, indeed, using the Transaction Pooler instead of the Direct connection solved the issue for me too.