Skip to content

Commit 8389f9b

Browse files
author
Marvin Zhang
committed
refactor: improve Vercel compatibility by handling SASL authentication and connection pooling in PostgreSQL configuration
1 parent 2a2fbc8 commit 8389f9b

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ NODE_ENV="development"
171171

172172
## ======== VERCEL-SPECIFIC CONFIGURATION ========
173173
# Vercel Postgres (automatically provided by Vercel)
174+
# The system automatically prefers POSTGRES_URL_NON_POOLING over POSTGRES_URL to avoid SASL authentication issues
174175
# POSTGRES_URL="postgres://default:[email protected]:5432/verceldb"
175176
# POSTGRES_PRISMA_URL="postgres://default:[email protected]:5432/verceldb?pgbouncer=true&connect_timeout=15"
176177
# POSTGRES_URL_NON_POOLING="postgres://default:[email protected]:5432/verceldb"

docs/guides/VERCEL_DEPLOYMENT.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ If you encounter SSL certificate errors in Vercel deployment:
100100
- If needed, you can override by setting `POSTGRES_SSL="false"` in Vercel environment variables
101101
- For custom SSL configuration, set `POSTGRES_SSL='{"rejectUnauthorized":false,"ca":"..."}'`
102102

103+
### SASL Authentication Errors (SCRAM-SERVER-FINAL-MESSAGE: server signature is missing)
104+
If you encounter SASL authentication errors:
105+
- This is automatically handled by using `POSTGRES_URL_NON_POOLING` when available
106+
- The system prefers direct connections over pooled connections to avoid authentication issues
107+
- Vercel automatically provides `POSTGRES_URL_NON_POOLING` which bypasses PgBouncer connection pooling
108+
- No manual configuration needed - the fix is automatic
109+
103110
### Auto-Detection Not Working
104111
- Check console logs for database detection messages
105112
- Ensure environment variable names match exactly: `POSTGRES_URL`, `MYSQL_URL`, `SQLITE_URL`

packages/core/src/utils/typeorm-config.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ function parseSSLConfig(sslEnvVar?: string): boolean | object {
6565
}
6666
}
6767

68+
/**
69+
* Create additional PostgreSQL connection options for Vercel compatibility
70+
*/
71+
function getPostgresExtraOptions(): any {
72+
return {
73+
// Handle Vercel's connection pooling and authentication issues
74+
connectionTimeoutMillis: 30000,
75+
idleTimeoutMillis: 30000,
76+
max: 1, // Limit connection pool size in serverless environment
77+
// Additional options for SASL authentication stability
78+
statement_timeout: 30000,
79+
idle_in_transaction_session_timeout: 30000,
80+
};
81+
}
82+
6883
/**
6984
* Get or create the singleton DataSource instance
7085
* All services should use this to ensure they share the same database connection
@@ -145,6 +160,7 @@ export function createDataSource({
145160
type: 'postgres',
146161
url: options.url,
147162
ssl: options.ssl ?? false,
163+
extra: getPostgresExtraOptions(),
148164
} as DataSourceOptions;
149165
} else {
150166
config = {
@@ -156,6 +172,7 @@ export function createDataSource({
156172
password: options.password,
157173
database: options.database,
158174
ssl: options.ssl ?? false,
175+
extra: getPostgresExtraOptions(),
159176
} as DataSourceOptions;
160177
}
161178
break;
@@ -191,7 +208,9 @@ export function createDataSource({
191208
* Parse database configuration from environment variables
192209
*/
193210
export function parseTypeORMConfig(): TypeORMStorageOptions {
194-
const postgresUrl = process.env.POSTGRES_URL;
211+
// For Vercel, prefer direct connection URLs that bypass connection pooling
212+
// to avoid SASL authentication issues
213+
const postgresUrl = process.env.POSTGRES_URL_NON_POOLING || process.env.POSTGRES_URL;
195214
const mysqlUrl = process.env.MYSQL_URL;
196215
const dbType = process.env.DEVLOG_STORAGE_TYPE?.toLowerCase();
197216

0 commit comments

Comments
 (0)