Vercel PHP serverless functions failing to connect to Supabase Postgres

Problem

I am building a web application using a modern frontend stack (Vite, TailwindCSS, Flowbite, and SweetAlert2) powered by a PHP backend. Initially, this project was developed and tested successfully on my local machine using a Laragon/MySQL setup.

To deploy the application on Vercel, I decided to upgrade my database and migrate from MySQL to Supabase (PostgreSQL). While the application and the Supabase connection work perfectly on my local environment, the database connection completely fails after deploying to Vercel, despite the frontend loading successfully.

Current Behavior

  • Frontend loads perfectly, but API calls to /api/beneficiaries.php return a 500 status.
  • My DB diagnostic endpoint confirms the following:
    • db_connection=pgsql
    • use_supabase=true
    • host/port/db/user/password are all set correctly
    • db_sslmode=require
    • pdo_pgsql_loaded=true

Despite this, it still fails with the error:

Error: Could not connect to database. Please check your configuration.

Expected Behavior

PHP API functions deployed on Vercel should successfully connect to the Supabase Postgres database and return data, exactly as they do on my local machine.

Steps to Reproduce

  1. Deploy project to Vercel with PHP functions (vercel-php@0.7.4) and Vite-built frontend.
  2. Set production environment variables:
    • DB_CONNECTION=pgsql
    • DB_HOST
    • DB_PORT=5432
    • DB_DATABASE
    • DB_USERNAME
    • DB_PASSWORD
    • DB_SSLMODE=require
    • APP_ENV=production
    • ENV_KEY=production
  3. Open the diagnostic endpoint: https://dole-system.vercel.app/api/auth.php?db_test=1
  4. The output returned is:
    {
      "success": false,
      "error": "Could not connect to database..."
    }
    

Project Information

  • URL: https://dole-system.vercel.app
  • Frontend: Vite, TailwindCSS, Flowbite, SweetAlert2
  • Backend: PHP API on Vercel Serverless (vercel-php@0.7.4)
  • Database: Supabase Postgres (Direct connection)
  • Runtime config confirmed via script:
    • db_connection=pgsql
    • use_supabase=true
    • db_sslmode=require
    • pdo_pgsql_loaded=true

Main Question

What are the most likely causes when PHP on Vercel reports all DB variables as present and the pgsql extension as loaded, but the PDO connection still fails to reach Supabase Postgres?

Is there any Vercel-specific networking, SSL, or connection-string requirement for connecting to Supabase using PHP serverless functions?

Hi JorgY_,

Given your diagnostic output, I’d focus on the Supabase connection type rather than the PHP env vars.

The suspicious part is:

Database: Supabase Postgres Direct connection
Port: 5432
pdo_pgsql_loaded: true
sslmode=require
Works locally, fails on Vercel

Supabase direct database connections use IPv6 unless you have the IPv4 add-on enabled. Vercel serverless functions are commonly the case where the direct db.<project-ref>.supabase.co:5432 connection fails, even though the same credentials work locally.

I’d switch the deployed PHP API to the Supabase Shared Pooler / Supavisor connection string instead of the direct connection string.

In Supabase:

Project → Connect → Connection string → Pooler

For serverless functions, try the transaction pooler first:

host: aws-<region>.pooler.supabase.com
port: 6543
user: postgres.<project-ref>
database: postgres
sslmode: require

So your PDO DSN would be shaped more like:

$dsn = "pgsql:host={$host};port=6543;dbname={$database};sslmode=require";
$pdo = new PDO($dsn, $username, $password, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

Make sure the username changes too. For the pooler it is usually postgres.<project-ref>, not just postgres.

Supabase explains the IPv4/IPv6 difference here:

One more thing: temporarily log the real PDO exception message, but not the password or full connection string. The difference between connection refused, timeout, and password authentication failed will confirm whether this is network/IPv6 or credentials.