Careeroza
Setup and Connection
Install and Configure
Example
npm i @nestjs/typeorm typeorm pgExample output
added 31 packages in 6s
src/app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProductsModule } from './products/products.module';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
type: 'postgres',
url: config.getOrThrow<string>('DATABASE_URL'),
autoLoadEntities: true, // entities registered with forFeature() are picked up
synchronize: false, // NEVER true in production: use migrations
logging: config.get('NODE_ENV') === 'development' ? ['query', 'error'] : ['error'],
}),
}),
ProductsModule,
],
})
export class AppModule {}Run PostgreSQL Locally
Example
Quickest way, with Docker (see the Docker material):
docker run -d --name shopease-db -e POSTGRES_USER=shopease -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=shopease -p 5432:5432 postgres:16Example output
Unable to find image 'postgres:16' locally 16: Pulling from library/postgres Status: Downloaded newer image for postgres:16 3f9c2a7d1e5b8c0a4d6f...
Tip: synchronize: true auto-creates tables from entities. It is handy for a 10-minute prototype, but it can drop columns and data. Use migrations for anything real.