fix: pass POSTGRES_PASSWORD into Docker for DB auth
Containers did not receive DATABASE_PASSWORD from .env; fall back to POSTGRES_PASSWORD and expose both vars in compose so SCRAM auth works when the password is not embedded in DATABASE_URL. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
9e75ec0e35
commit
71db1a9718
@ -30,7 +30,7 @@ function resolveDatabaseUrl(raw: string): string {
|
||||
const dockerHost = process.env.DATABASE_DOCKER_HOST || 'host.docker.internal';
|
||||
console.warn(
|
||||
`[db] DATABASE_URL host "${parsed.hostname}" does not work inside Docker; using "${dockerHost}". ` +
|
||||
'Set DATABASE_URL=@host.docker.internal in .env to silence this.'
|
||||
'Set DATABASE_URL with host.docker.internal in .env to silence this.'
|
||||
);
|
||||
parsed.hostname = dockerHost;
|
||||
return parsed.toString();
|
||||
@ -41,6 +41,21 @@ function resolveDatabaseUrl(raw: string): string {
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
/** Resolves DB password: explicit env vars beat URL (avoids encoding issues in DATABASE_URL). */
|
||||
function resolveDatabasePassword(parsed: URL): string {
|
||||
const fromUrl = parsed.password ? decodeURIComponent(parsed.password) : '';
|
||||
for (const candidate of [
|
||||
process.env.DATABASE_PASSWORD,
|
||||
process.env.POSTGRES_PASSWORD,
|
||||
fromUrl,
|
||||
]) {
|
||||
if (typeof candidate === 'string' && candidate !== '') {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds pg Pool configuration from DATABASE_URL, preferring DATABASE_PASSWORD when set.
|
||||
* @returns Parsed host/port config or a raw connectionString fallback
|
||||
@ -55,16 +70,13 @@ function getPoolConfig(): { connectionString: string } | { host: string; port: n
|
||||
}
|
||||
try {
|
||||
const parsed = new URL(url);
|
||||
// Use DATABASE_PASSWORD if set (avoids URL-encoding issues and empty password in URL)
|
||||
const passwordFromEnv = process.env.DATABASE_PASSWORD;
|
||||
const passwordFromUrl = parsed.password ? decodeURIComponent(parsed.password) : '';
|
||||
const password = typeof passwordFromEnv === 'string' && passwordFromEnv !== '' ? passwordFromEnv : passwordFromUrl;
|
||||
const password = resolveDatabasePassword(parsed);
|
||||
return {
|
||||
host: parsed.hostname,
|
||||
port: parsed.port ? parseInt(parsed.port, 10) : 5432,
|
||||
database: parsed.pathname?.slice(1) || 'crypto_wallet',
|
||||
user: parsed.username || process.env.USER || 'postgres',
|
||||
password: typeof password === 'string' ? password : '',
|
||||
user: decodeURIComponent(parsed.username || '') || process.env.POSTGRES_USER || 'postgres',
|
||||
password,
|
||||
max: 20,
|
||||
idleTimeoutMillis: 30000,
|
||||
connectionTimeoutMillis: 10000,
|
||||
|
||||
@ -23,6 +23,12 @@ async function migrate() {
|
||||
' Example: DATABASE_URL=postgresql://postgres:PASSWORD@host.docker.internal:5432/games_platform_db'
|
||||
);
|
||||
}
|
||||
const msg = err && typeof err === 'object' && 'message' in err ? String((err as { message?: string }).message) : '';
|
||||
if (msg.includes('client password must be a string') || msg.includes('password authentication failed')) {
|
||||
console.error(
|
||||
' Set POSTGRES_PASSWORD (and optionally DATABASE_PASSWORD) in .env — they must be passed into Docker containers.'
|
||||
);
|
||||
}
|
||||
console.error('Migration failed:', err);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@ -22,6 +22,9 @@ x-backend-common: &backend-common
|
||||
# Depuis les conteneurs : host.docker.internal (pas localhost).
|
||||
# Définissez DATABASE_URL dans .env pour contrôler l’URL complète (recommandé sur VPS).
|
||||
DATABASE_URL: ${DATABASE_URL:-postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@host.docker.internal:${POSTGRES_PORT:-5432}/${POSTGRES_DB}}
|
||||
DATABASE_PASSWORD: ${DATABASE_PASSWORD:-}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
POSTGRES_USER: ${POSTGRES_USER:-postgres}
|
||||
REDIS_URL: redis://redis:6379
|
||||
REDIS_ENABLED: "true"
|
||||
REDIS_ROUND_CACHE: "true"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user