I am writing quiz api and I have problem with connection to the postgres database
app.module:
@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: './schema.gql',
playground: true,
}),
TypeOrmModule.forRoot({
keepConnectionAlive: true,
type: 'postgres',
port: 5433,
username: 'postgres',
password: '12345',
database: 'postgres',
autoLoadEntities: true,
synchronize: false,
}),
QuizModule,
QuestionModule,
AnswerModule
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
docker-compose:
version: "3.9"
services:
postgres:
image: postgres:13-alpine
restart: always
env_file:
- .env
environment:
- POSTGRES_USER=$POSTGRES_USER
- POSTGRES_PASSWORD:$POSTGRES_PASSWORD
ports:
- "$POSTGRES_PORT:$POSTGRES_PORT_DOCKER"
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- db_network
volumes:
postgres_data:
networks:
db_network:
.env:
POSTGRES_USER=postgres
POSTGRES_URL=postgresql://postgres:12345@localhost:5432/postgres?schema=public
POSTGRES_PASSWORD=12345
POSTGRES_PORT_DOCKER=5433
POSTGRES_PORT=5433
I dont know what is wrong but i have:
[Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [NestFactory] Starting Nest application...
[Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] TypeOrmModule dependencies initialized +24ms
[Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] QuizModule dependencies initialized +1ms
[Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] QuestionModule dependencies initialized +0ms
[Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] AnswerModule dependencies initialized +0ms
[Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] AppModule dependencies initialized +0ms
[Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] GraphQLSchemaBuilderModule dependencies initialized +0ms
[Nest] 34393 - 07/10/2023, 10:16:39 PM LOG [InstanceLoader] GraphQLModule dependencies initialized +0ms
[Nest] 34393 - 07/10/2023, 10:16:39 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
Error: Connection terminated unexpectedly
my application was working before adding entities but after removing it still doesn't work Could the problem be caused by something other than the database connection itself?
version: "3.9"
services:
postgres:
image: postgres:13-alpine
restart: always
env_file:
- .env
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=postgres
ports:
- "${POSTGRES_PORT}:${POSTGRES_PORT_DOCKER}"
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- db_network
volumes:
postgres_data:
networks:
db_network:
fix POSTGRES_USER=${POSTGRES_USER} to use correct variables