Laravel 10.x部署Vercel连接Postgres报错:Endpoint ID未指定
问题描述
基于Laravel 10.x开发私有应用,采用Headless PHP部署至Vercel并使用Vercel Postgres数据库,但数据库连接失败。执行简单查询时触发错误:
SQLSTATE[08006] [7] ERROR: Endpoint ID is not specified. Either please upgrade the postgres client library (libpq) for SNI support or pass the endpoint ID (first part of the domain name) as a parameter: '?options=endpoint%3D
'. (Connection: pgsql, SQL: select * from "newsletters")
已在Vercel环境变量及vercel.json中配置数据库参数,但问题仍未解决,相关配置如下:
vercel.json 配置
{ "version": 2, "functions": { "api/index.php": { "runtime": "vercel-php@0.6.0" } }, "routes": [ { "src": "/(.*)", "dest": "/api/index.php" } ], "env": { "APP_NAME": "XXXXXXXX", "APP_ENV": "local", "APP_DEBUG": "true", "APP_URL": "XXXXXXXXX", "DB_CONNECTION": "pgsql", "DB_HOST": "XXXXXXXXXXXXXXXX.us-east-1.postgres.vercel-storage.com", "DB_PORT": "5432", "DB_DATABASE": "verceldb", "DB_USERNAME": "default", "DB_PASSWORD": "XXXXXXXX", "APP_CONFIG_CACHE": "/tmp/config.php", "APP_EVENTS_CACHE": "/tmp/events.php", "APP_PACKAGES_CACHE": "/tmp/packages.php", "APP_ROUTES_CACHE": "/tmp/routes.php", "APP_SERVICES_CACHE": "/tmp/services.php", "VIEW_COMPILED_PATH": "/tmp", "CACHE_DRIVER": "array", "LOG_CHANNEL": "stderr", "SESSION_DRIVER": "cookie", "VIEW_COMPILED_PATH": "/tmp/views", "SSR_TEMP_PATH": "/tmp/ssr", "NODE_PATH": "node", "CI": "false" } }
/api/index.php 代码
<?php // This is the front controller for the API. It routes all requests to the public/index.php file. require __DIR__ . '/../public/index.php';
解决方案
方案1:手动指定Endpoint ID
从DB_HOST中提取Endpoint ID(即域名的第一部分,例如XXXXXXXXXXXXXXXX.us-east-1.postgres.vercel-storage.com中的XXXXXXXXXXXXXXXX),通过以下两种方式配置:
方式A:新增环境变量并修改Laravel数据库配置
- 在
vercel.json的env中添加DB_OPTIONS:
"DB_OPTIONS": "endpoint=XXXXXXXXXXXXXXXX"
- 修改Laravel项目的
config/database.php,在pgsql连接配置中加入options项:
'pgsql' => [ // 原有配置保持不变 'options' => env('DB_OPTIONS', ''), ],
方式B:直接拼接参数到数据库名
修改vercel.json中的DB_DATABASE,拼接Endpoint ID参数:
"DB_DATABASE": "verceldb?options=endpoint%3DXXXXXXXXXXXXXXXX"
方案2:升级Vercel PHP运行时
当前使用的vercel-php@0.6.0可能依赖的libpq版本不支持SNI,升级到最新版运行时:
"functions": { "api/index.php": { "runtime": "vercel-php@0.7.0" } // 使用最新稳定版 }
新版本运行时通常已包含支持SNI的libpq,无需额外配置Endpoint ID。
额外验证步骤
- 在Vercel控制台重新部署,勾选「Redeploy with clean cache」,确保环境变量生效。
- 确认Laravel配置缓存已更新,由于
APP_CONFIG_CACHE指向/tmp目录,部署时会自动生成新缓存,但强制清理缓存可避免旧配置残留。
内容的提问来源于stack exchange,提问作者Trusty

