{"record":{"id":"6d5028eee6fbbec6","repo":"mem0ai/mem0","slug":"pgvector-requires-either-connectionstring-or-mis","errorCode":null,"errorMessage":"PGVector requires either connectionString or ${missingFields.join(\", \")}","messagePattern":"PGVector requires either connectionString or (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"mem0-ts/src/oss/src/vector_stores/pgvector.ts","lineNumber":186,"sourceCode":"  hnsw?: boolean;\n}\n\nfunction getConnectionString(config: PGVectorConfig): string | undefined {\n  return config.connectionString?.trim() || undefined;\n}\n\nfunction validateConnectionConfig(config: PGVectorConfig): void {\n  if (getConnectionString(config)) {\n    return;\n  }\n\n  const missingFields = [\"user\", \"password\", \"host\", \"port\"].filter((field) => {\n    const v = config[field as keyof PGVectorConfig];\n    return v === undefined || v === null || v === \"\";\n  });\n\n  if (missingFields.length > 0) {\n    throw new Error(\n      `PGVector requires either connectionString or ${missingFields.join(\", \")}`,\n    );\n  }\n}\n\nfunction buildClientConfig(\n  config: PGVectorConfig,\n  database?: string,\n): ClientConfig {\n  const connectionString = getConnectionString(config);\n  if (connectionString) {\n    return {\n      connectionString,\n      ...(config.ssl !== undefined ? { ssl: config.ssl } : {}),\n    };\n  }\n\n  return {","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/vector_stores/pgvector.ts#L168-L204","documentation":"PgVector accepts either a connectionString or discrete connection fields. If no usable connection string is found and any of user, password, host, port is missing/empty, the constructor throws this error listing the missing fields. It fails fast before any network call is attempted.","triggerScenarios":"Constructing PgVector with only { host, port, database } (no user/password and no connectionString), passing an empty-string connectionString, or reading config from env vars that are undefined (e.g. process.env.PGHOST set but PGUSER/PGPASSWORD unset when building the discrete config).","commonSituations":"Switching from a connection URL to individual env vars in Docker/Kubernetes and forgetting one; a .env file not loaded so PG* vars are undefined; trailing whitespace or empty strings in config; deploying with a partially templated secret.","solutions":["Provide a full connection string: new PgVector({ connectionInfo: 'postgres://user:pass@host:5432/db', ... })","Or supply all discrete fields: user, password, host, port (and optionally database)","Check that the env vars feeding the config are actually set in the runtime environment (docker-compose env, k8s secrets, dotenv loaded)","Verify none of the values are empty strings — empty counts as missing"],"exampleFix":"// before\nconst vs = new PgVector({ host: 'db', port: 5432, database: 'mem0' });\n\n// after\nconst vs = new PgVector({\n  user: 'mem0',\n  password: process.env.PGPASSWORD,\n  host: 'db',\n  port: 5432,\n  database: 'mem0',\n});","handlingStrategy":"validation","validationCode":"function validatePgConfig(c: any): void {\n  const cs = c?.connectionString ?? c?.connectionInfo;\n  if (cs && String(cs).trim() !== '') return;\n  const missing = ['user','password','host','port'].filter(f => !c?.[f]);\n  if (missing.length) throw new Error(`PG config missing: ${missing.join(', ')}`);\n}\nvalidatePgConfig(pgConfig);","typeGuard":"const hasPgCredentials = (c: any): boolean =>\n  !!(c?.connectionString ?? c?.connectionInfo) ||\n  ['user','password','host','port'].every(f => !!c?.[f]);","tryCatchPattern":"try { const vs = new PgVector(pgConfig); } catch (e) { if (e instanceof Error && e.message.includes('PGVector requires')) { /* fail deployment config check, do not retry */ } throw e; }","preventionTips":["Prefer a single CONNECTION STRING env var over four discrete ones","Fail fast at app boot: assert required env vars exist before constructing stores","Include PG config in a smoke test that constructs the store in CI"],"tags":["pgvector","postgres","connection","configuration","environment","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}