TypeORMでPostgreSQLを使う
うまく行ったパターン
構成
code:.env
# for typeorm
TYPEORM_CONNECTION=postgres
TYPEORM_HOST=localhost
TYPEORM_PORT=5432
TYPEORM_USERNAME=admin
TYPEORM_PASSWORD=admin
TYPEORM_DATABASE=databasename
TYPEORM_SYNCHRONIZE=false
TYPEORM_LOGGING=true
TYPEORM_ENTITIES=dist/**/*.entity.js
TYPEORM_MIGRATIONS=dist/migrations/*.js
TYPEORM_MIGRATIONS_DIR=src/migrations
# for docker
POSTGRES_USER=admin
POSTGRES_PASSWORD=admin
POSTGRES_DB=databasename
PGDATA: /data/postgres
PGADMIN_DEFAULT_EMAIL=pgadmin4@pgadmin.org
PGADMIN_DEFAULT_PASSWORD=admin
code:docker-compose.yml
version: '3'
services:
postgres:
container_name: postgres_container
image: postgres
env_file:
- .env
volumes:
- postgres:/data/postgres
ports:
- '5432:5432'
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
env_file:
- .env
volumes:
- pgadmin:/root/.pgadmin
ports:
- '5050:80'
networks:
- postgres
restart: unless-stopped
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:
typeorm migration:showがコケなければOK
優先順位がある
1. 環境変数
.envがあればそこからロードする
TYPEORM_CONNECTIONまたはTYPEORM_URLが設定されているときに使われる?
2. ormconfig.env
3. ormconfig.[format], 優先順位は次の順[js, ts, json, yml, yaml, xml]
ormconfig.jsonを用意していたのに読み込まれなかった
別用途で.envを用意していて、ormconfig.jsonが読み込まれない状況に陥っていたと思われる
発生したエラー
PostgreSQLのclient入れろ的なエラー
$ typeorm migration:show で怒られる
文言メモってない
$ npm install pgで解決
typeORMのCLI実行時に、設定ファイルを読み込めていない
code:sh
$ typeorm migration:show
Error during migration show:
error: password authentication failed for user "username"
usernameにはWSL2のユーザー名が表示された
設定ファイル
更新 .env
削除 typeorm.json
.envにTYPEORM_*の環境変数を設定する
typeORMのCLI実行時に、設定ファイルを読み込めているが、認証に失敗する
code:sh
$ typeorm migration:show
Error during migration show:
error: password authentication failed for user "admin"
たしか...原因は一度docker volumeを作ってから、yamlファイル上のuser/passを変えていたから
docker volumeを作り直して解決したと思う
たしかpgadminがご機嫌ナナメだったと思う
いろいろ試行錯誤していたので記録がない
名前付きのvolumeをpostgres用、pgadmin用に2つ作って、それぞれのサービスのデータを永続化する 参考にしたdocker-compose.yml
textにlengthは設定できないぞってエラー
SQLiteでは設定できるけど、PostgreSQLは設定できない
code:diff
- @Column({ type: 'text', length: 500, default: '' })
+ @Column({ type: 'text', default: '' })
NestJSの書き方が悪いと思っていろいろ試行錯誤した痕跡がある
code:app.module.ts(diff)
+ import { DatabaseModule } from './database/database.module';
@Module({
imports: [
TypeOrmModule.forRoot(),
ConfigModule.forRoot({
+ envFilePath: '.env',
isGlobal: true,
}),
..
+ DatabaseModule,
],
providers: [
..
code:database.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
TypeOrmModule.forRootAsync({
useFactory: (configService: ConfigService) => {
console.log(configService.get('POSTGRES_USER'));
return {
// eslint-disable-next-line @typescript-eslint/prefer-as-const
type: 'postgres' as 'postgres',
host: configService.get('TYPEORM_HOST'),
port: Number(configService.get('TYPEORM_PORT')),
username: configService.get('TYPEORM_USERNAME'),
password: configService.get('TYPEORM_PASSWORD'),
database: configService.get('TYPEORM_DATABASE'),
synchronize: configService.get('TYPEORM_SYNCHRONIZE'),
logging: configService.get('TYPEORM_LOGGING'),
cli: {
migrationsDir: configService.get('TYPEORM_MIGRATIONS_DIR'),
},
} as TypeOrmModuleOptions;
},
}),
],
})
export class DatabaseModule {}