dokploy icon indicating copy to clipboard operation
dokploy copied to clipboard

Database migration fails with "password authentication failed" when using a custom POSTGRES_PASSWORD

Open khanhquocnguyen opened this issue 1 year ago • 2 comments

To Reproduce

Steps to reproduce the behavior:

  1. Set up a docker-compose.yml and .env file for a fresh Dokploy installation.
  2. In the .env file, define a custom POSTGRES_PASSWORD (any password other than the default amukds4wi9001583845717ad2).
  3. In docker-compose.yml, configure the postgres service to use ${POSTGRES_PASSWORD}.
  4. Configure the dokploy service to use the same ${POSTGRES_PASSWORD} within its DATABASE_URL environment variable.
  5. Ensure a clean state by running docker compose down --volumes.
  6. Start the services with docker compose up -d.
  7. Check the logs of the dokploy container using docker logs <dokploy_container_name>.

Current vs. Expected behavior

The migration script fails with the following error, indicating it tried to authenticate with an incorrect password:

Migration failed [Error [PostgresError]: password authentication failed for user "dokploy"] {
  severity_local: 'FATAL',
  severity: 'FATAL',
  code: '28P01',
  file: 'auth.c',
  line: '321',
  routine: 'auth_failed'
}

This is immediately followed by a server error because the database tables were not created:

Main Server Error [Error [PostgresError]: relation "member" does not exist] {
  severity_local: 'ERROR',
  severity: 'ERROR',
  code: '42P01',
  ...
}

Expected behavior The Dokploy service should successfully read the password from the DATABASE_URL environment variable, connect to the PostgreSQL database, and run the migrations without errors.

Provide environment information

OS: Ubuntu 24.04 - AMD64
Docker Image: dokploy/dokploy:v0.24.4

Which area(s) are affected? (Select all that apply)

Docker Compose

Are you deploying the applications where Dokploy is installed or on a remote server?

Both

Additional context

When installing Dokploy using Docker Compose, if a custom password is set for the PostgreSQL database via the POSTGRES_PASSWORD environment variable, the database migration script fails with a password authentication failed error. It appears the migration script ignores the provided password in the DATABASE_URL and attempts to authenticate using the hardcoded default password instead.

We performed extensive debugging and can confirm the following:

  • Manual connection works: We were able to manually connect to the Postgres container from another container on the same network using psql and the custom password. This proves that the Postgres container is set up correctly and the password variable is correct.
  • The issue is specific to the migration script: The failure happens consistently at the Migration failed step.
  • The default password works: The migration succeeds only if we change the POSTGRES_PASSWORD in the .env file to the default password (amukds4wi9001583845717ad2).

This strongly suggests that the migration script is not respecting the provided DATABASE_URL, and is instead using a hardcoded default password for authentication.

Workaround The only current workaround is to use the default Dokploy password for the POSTGRES_PASSWORD environment variable.

Root cause

I have found the root cause of this issue after inspecting the source code and the build process. The problem lies within the Dockerfile and how the migration script is executed.

Dockerfile copies a static .env file: The Dockerfile used to build the dokploy image contains the instruction COPY .env.production ./.env. This action bakes the default production environment variables directly into the final image as a .env file.

Migration script prioritizes the baked-in .env file: The migration is run using the npm script "migration:run": "tsx -r dotenv/config migration.ts". The -r dotenv/config flag explicitly tells the script to load variables from the .env file that was copied into the image.

This causes the migration script to always use the default DATABASE_URL (with the default password) from the baked-in .env file, while completely ignoring the DATABASE_URL or any other database-related environment variables provided externally by Docker Compose.

This is the direct cause of the password authentication failed error when a custom password is set, as the migration script attempts to connect with the default password while the Postgres container has been initialized with the custom one.

Will you send a PR to fix it?

No

khanhquocnguyen avatar Jul 24 '25 09:07 khanhquocnguyen

I also got the same issue like @khanhquocnguyen when following the Manual Installation link and use a custom password.

However the issue was not caused by server loading DATABASE_URL from .env file, since dotenv/config by default does not replace existing environment variables unless override option is set to true (ref.

The actual root cause lies in the build system which uses esbuild: https://github.com/Dokploy/dokploy/blob/canary/apps/dokploy/esbuild.config.ts

With the use of define, esbuild will replace process.env.DATABASE_URL with the values from .env.production in dist/server.mjs output, thus effectively hard-coded the database credentials.

The suggested solution is to remove the define option from esbuild.config.ts.

@Siumauricio could you help take a look at this?

nguyenlc1993 avatar Aug 12 '25 15:08 nguyenlc1993

Thank you for the details, I will try to take a look this week

Siumauricio avatar Aug 22 '25 04:08 Siumauricio