Database migration fails with "password authentication failed" when using a custom POSTGRES_PASSWORD
To Reproduce
Steps to reproduce the behavior:
- Set up a
docker-compose.ymland.envfile for a fresh Dokploy installation. - In the
.envfile, define a customPOSTGRES_PASSWORD(any password other than the defaultamukds4wi9001583845717ad2). - In
docker-compose.yml, configure the postgres service to use${POSTGRES_PASSWORD}. - Configure the dokploy service to use the same
${POSTGRES_PASSWORD}within itsDATABASE_URLenvironment variable. - Ensure a clean state by running
docker compose down --volumes. - Start the services with
docker compose up -d. - 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_PASSWORDin 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
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?
Thank you for the details, I will try to take a look this week