- On Windows WSL
I have a Node TypeScript application that runs on my local machine but also connects to a local Postgres database that gets created in Docker. So the DB is in Docker but the app is on my local machine.
when I npm run start:dev
it will init the db with the docker-compose.yaml, then use npm typeorm to run the migrations, then nx to serve the application.
-
My init-db script is successfully spinning up the Docker Postgres instance and creates the db
-
when trying to run the migrations I’m getting the following error:
Error during migration run:
error: password authentication failed for user "postgres"
...........
{
length: 104,
severity: 'FATAL',
code: '28P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'auth.c',
line: '321',
routine: 'auth_failed'
}
all the passwords and configurations are correct in my env files
got the container IP for the postgres instance and tried to connect to it directly through terminal psql command, and it would time out. the only way to get into the db through psql was to actually go to the terminal within the Docker application inside the container.
i think the most obvious solution is to Dockerize all the things but that can’t be done right now with the team I’m working on. All my other team members are on Mac and have no issues, the only difference in our setups is that I’m on Windows WSL.
Is there perhaps some kind of permissions configuration I need to do in Docker to allow local applications outside of the container to connect??
// docker-compose.yaml
version: '3.8'
services:
postgres:
container_name: postgres
image: postgres:15
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
volumes:
- ./init-database.sh:/docker-entrypoint-initdb.d/init-database.sh
ports:
- '5432:5432'
// .env file
....
# LOCAL DB
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=secret
DB_NAME=tracker
// ormconfig.ts
import { DataSource } from 'typeorm';
import { config } from 'dotenv';
config();
export default new DataSource({
type: 'postgres',
host: process.env.DB_HOST || 'localhost',
port: Number(process.env.DB_PORT) || 5432,
username: process.env.DB_USER || 'postgres',
password: process.env.DB_PASSWORD || 'secret',
database: process.env.DB_NAME || 'tracker',
entities: [process.env.ENV === 'local' ? 'dist/**/entities/**/*.js' : 'src/**/entities/**/*.js'],
migrations: [process.env.ENV === 'local' ? 'dist/**/migrations/**/*.js' : 'src/**/migrations/**/*.js'],
synchronize: false,
ssl: process.env.ENV !== 'local',
});
// package.json scripts
// running npm run start:dev
"scripts": {
"start:dev": "docker-compose up -d && npm run migration:run && nx run-many -t serve",
"typeorm": "npx typeorm-ts-node-commonjs",
"migration:run": "nx build redacted-backend && npm run typeorm -- --dataSource=apps/redacted-backend/ormconfig.ts migration:run",
},
- npm installed all deps
- npm, node, nvm all up to date
- clearing out docker containers between attempts
- tried using
localhost
and127.0.0.1
andpostgresdb
and<container-ip>
for the connection string - tried using “password” everywhere for the postgres_password instead of “secret” since “password” is the default
- uninstalled and reinstalled postgres in WSL terminal
- manually set postgres password thru psql in the Docker terminal
Davis Arnoodle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1