:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(nodejs): require a database url only where postgres is opened (#129227)
Tue Haulunda1499b026b60
diff --git a/nodejs/src/common/config/config.ts b/nodejs/src/common/config/config.tsindex acf5234d88a0..d8b18bfe9c9f 100644--- a/nodejs/src/common/config/config.ts+++ b/nodejs/src/common/config/config.ts@@ -57,7 +57,10 @@ export function overrideWithEnv(}const newConfig: PluginsServerConfig = { ...tmpConfig }- newConfig.DATABASE_URL = buildPosthogDatabaseUrl(newConfig)+ // A service that never opens Postgres runs without database settings, so PostgresRouter reports a missing URL instead.+ if (newConfig.DATABASE_URL || newConfig.POSTHOG_DB_NAME) {+ newConfig.DATABASE_URL = buildPosthogDatabaseUrl(newConfig)+ }newConfig.BEHAVIORAL_COHORTS_DATABASE_URL = buildBehavioralCohortsDatabaseUrl(newConfig)diff --git a/nodejs/src/common/config/database-url.ts b/nodejs/src/common/config/database-url.tsindex 419f9f6b73ac..aff0375d65ea 100644--- a/nodejs/src/common/config/database-url.ts+++ b/nodejs/src/common/config/database-url.ts@@ -18,15 +18,16 @@ export type BehavioralCohortsDatabaseUrlConfig = Pick<| 'POSTGRES_BEHAVIORAL_COHORTS_PASSWORD'>+export const MISSING_DATABASE_URL_MESSAGE =+ 'You must specify either DATABASE_URL or the database options POSTHOG_DB_NAME, POSTHOG_DB_USER, POSTHOG_DB_PASSWORD, POSTHOG_POSTGRES_HOST, POSTHOG_POSTGRES_PORT!'+export function buildPosthogDatabaseUrl(config: PosthogDatabaseUrlConfig): string {if (config.DATABASE_URL) {return config.DATABASE_URL}if (!config.POSTHOG_DB_NAME) {- throw Error(- 'You must specify either DATABASE_URL or the database options POSTHOG_DB_NAME, POSTHOG_DB_USER, POSTHOG_DB_PASSWORD, POSTHOG_POSTGRES_HOST, POSTHOG_POSTGRES_PORT!'- )+ throw Error(MISSING_DATABASE_URL_MESSAGE)}const encodedUser = encodeURIComponent(config.POSTHOG_DB_USER)diff --git a/nodejs/src/common/utils/db/postgres.ts b/nodejs/src/common/utils/db/postgres.tsindex 152be7555eea..98a4e0706544 100644--- a/nodejs/src/common/utils/db/postgres.ts+++ b/nodejs/src/common/utils/db/postgres.ts@@ -2,6 +2,7 @@import { DateTime } from 'luxon'import { Client, Pool, PoolClient, QueryConfig, QueryResult, QueryResultRow, types as pgTypes } from 'pg'+import { MISSING_DATABASE_URL_MESSAGE } from '~/common/config/database-url'import { withSpan } from '~/common/tracing/tracing-utils'import { logger } from '../logger'@@ -104,6 +105,10 @@ export class PostgresRouter {constructor(serverConfig: PostgresRouterConfig, appName?: string) {installPostgresTypeParsers()+ if (!serverConfig.DATABASE_URL) {+ throw Error(MISSING_DATABASE_URL_MESSAGE)+ }+const app_name = appName ?? 'unknown'logger.info('🤔', `Connecting to common Postgresql...`)const commonClient = createPostgresPool(diff --git a/nodejs/tests/config.test.ts b/nodejs/tests/config.test.tsindex 86ac72e18d06..37c5ffac6fad 100644--- a/nodejs/tests/config.test.ts+++ b/nodejs/tests/config.test.ts@@ -1,4 +1,6 @@import { buildIntegerMatcher, buildStringMatcher, getDefaultConfig, overrideWithEnv } from '~/common/config/config'+import { MISSING_DATABASE_URL_MESSAGE } from '~/common/config/database-url'+import { PostgresRouter } from '~/common/utils/db/postgres'describe('config', () => {test('overrideWithEnv 1', () => {@@ -29,14 +31,15 @@ describe('config', () => {})describe('DATABASE_URL', () => {- test('Error if DATABASE_URL is not set AND POSTHOG_DB_NAME is not set', () => {+ test('Error only when Postgres is opened if DATABASE_URL is not set AND POSTHOG_DB_NAME is not set', () => {const env = {DATABASE_URL: '',POSTHOG_DB_NAME: '',}- expect(() => overrideWithEnv(getDefaultConfig(), env)).toThrow(- 'You must specify either DATABASE_URL or the database options POSTHOG_DB_NAME, POSTHOG_DB_USER, POSTHOG_DB_PASSWORD, POSTHOG_POSTGRES_HOST, POSTHOG_POSTGRES_PORT!'- )+ const config = overrideWithEnv(getDefaultConfig(), env)++ expect(config.DATABASE_URL).toEqual('')+ expect(() => new PostgresRouter(config)).toThrow(MISSING_DATABASE_URL_MESSAGE)})test('Set DATABASE_URL to a string composed of URL-encoded connection options if DATABASE_URL is not explictly set', () => {