Skip to content

Conversation

@nh4ttruong
Copy link

Fix: DB_CLIENT mapping for PostgreSQL

Problem

When using databaseEngine: postgresql in values.yaml, the Helm chart sets DB_CLIENT=postgresql in the ConfigMap. However, Directus uses Knex.js as its database client, and Knex.js expects pg (not postgresql) as the client name for PostgreSQL connections.

This causes Directus to fail on startup with the following error:

[02:02:15.949] INFO: Installing Directus system tables...
Error: Database is already installed
    at runSeed (file:///directus/node_modules/.pnpm/@directus+api@file+api/node_modules/@directus/api/dist/database/seeds/run.js:13:15)
    at async Command.bootstrap (file:///directus/node_modules/.pnpm/@directus+api@file+api/node_modules/@directus/api/dist/cli/commands/bootstrap/index.js:18:9)

The error is misleading - it's not actually about the database being installed, but rather that Knex.js doesn't recognize postgresql as a valid client and the bootstrap process fails.

Evidence from Directus Source Code

From api/src/database/index.ts:

case 'cockroachdb':
case 'pg':  // <-- Knex expects 'pg', not 'postgresql'
    if (!connectionString) {
        requiredEnvVars.push('DB_HOST', 'DB_PORT', 'DB_DATABASE', 'DB_USER');
    }
    break;

Solution

Updated templates/configmap.yaml to support all common PostgreSQL aliases and map them to pg:

# Before
DB_CLIENT: {{ .Values.databaseEngine }}

# After  
DB_CLIENT: {{- if or (eq .Values.databaseEngine "postgresql") (eq .Values.databaseEngine "pg") (eq .Values.databaseEngine "postgres") }} pg {{- else }} {{ .Values.databaseEngine }} {{- end }}

This maintains backward compatibility and adds flexibility:

  • mysqlmysql (unchanged)
  • postgresqlpg (fixed, backward compatible)
  • postgrespg (new alias)
  • pgpg (new alias)

Testing

Tested with helm template:

$ helm template test . --set databaseEngine=postgresql | grep DB_CLIENT
  DB_CLIENT: pg

$ helm template test . --set databaseEngine=postgres | grep DB_CLIENT
  DB_CLIENT: pg

$ helm template test . --set databaseEngine=pg | grep DB_CLIENT
  DB_CLIENT: pg

$ helm template test . --set databaseEngine=mysql | grep DB_CLIENT
  DB_CLIENT: mysql

All PostgreSQL aliases correctly map to pg. Application starts successfully.

Checklist

  • Tested locally with all 3 PostgreSQL aliases
  • Documentation updated (README.md)

Knex.js expects 'pg' as the client name for PostgreSQL.
Support both 'pg' and 'postgres' as databaseEngine values per Directus docs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant