This is the env file code that I am using to run the docker container:
POSTGRES_HOST="localhost"
POSTGRES_PORT=5432
POSTGRES_USERNAME="postgres"
POSTGRES_PASSWORD="123"
POSTGRES_DATABASE="netabe"
This is the docker.compose.yml file:
postgres:
container_name: postgres_container
image: 'postgres:latest'
environment:
POSTGRES_USER: ${POSTGRES_USERNAME}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DATABASE}
ports:
- 5432:5432
networks:
app_network:
ipv4_address: 172.26.0.10
restart: unless-stopped
healthcheck:
test: ['CMD', 'pg_isready -U postgres']
interval: 5s
timeout: 5s
retries: 5
volumes:
- postgres:/data/postgres
It runs the postgres in docker container successfully but when I run the nest app then it says [Nest] 13620 - 05/01/2023, 6:56:41 pm ERROR [SequelizeModule] Unable to connect to the database. Retrying (1)....
I don't understand why this is happening. My postgres is also up and running and the database is also created in postgres but in the console it is throwing an error. The start:dev command: NODE_ENV=development nest start --watch
The postgresqlProvider is a different module and then I imported this in my app.module.ts file. the postgresqlprovider module code:
@Module({
imports: [
SequelizeModule.forRootAsync({
imports: [PostgresConfigModule],
useFactory: async (postgresConfigService: PostgresConfigService) => ({
dialect: 'postgres' as string,
host: postgresConfigService.host as string,
port: postgresConfigService.port,
username: postgresConfigService.username,
password: postgresConfigService.password,
database: postgresConfigService.database,
keepConnectionAlive: true,
synchronize: true,
autoLoadModels: true,
}),
inject: [PostgresConfigService],
} as SequelizeModuleAsyncOptions),
],
})
export class PostgresDatabaseProviderModule {}
It has nothing to do with the docker however the problem I found is with the synchronize option. Whenever it is false it works just fine and does not throw any error but when it is set to true then it gives the error that I mentioned above. I don't understand why this is happening.