Azure Postgresql server 설치 및 연결

8950 단어 AzurePostgreSQLAzure

Azure postgres 업로드

azure paas postgres 그중에서도 flexible 서버를 사용한다. single 서버를 사용해도 무방했지만, single서버는 11이하의 버전만 지원해, 현재 우리가 사용하는 postgres에서 마이그레이션을 위해 12버전이상의 postgres가 필요했다.

azure paas postgres server(이하 postgres 서버)를 만들어 app 서버와 연결했는데, 연결이 안된다..?

아래는 app 서버에서 postgres 서버로 연결을 시도한 결과이다.

error: no pg_hba.conf entry for host "--.--.--.--", user "ttv_postgres", database "ttv", SSL off

postgres 서버에 app 서버 정보가 없어 접속이 되지 않는다고한다.

왜 정보가 없을까?

  1. 네트워킹에서 app서버 접근을 허용하지 않은경우

방화벽 규칙에 ip를 등록했지만 같은 에러가발생한다.
  1. 그럼 내가 설정을 잘못한것이라, 프라이빗 엑세스로 시도하면되겠지?

    연결방법을 바꾸려면 postgres 서버를 따로 만들어줘야한다.

    (보안때문에 사진을 첨부할 수 없다)(여기서 subnet을 따로 만들어주지 않아서 한참헤맸다, subnet을 다른 곳에 붙이면 postgres서버가 배포되지 않는다. subnet은 꼭 따로만들어주자)

    프라이빗 엑세스로 시도해도 host이름만 private ip로 바뀔뿐 안된다.

  2. 답은 postgresSQL client tools와 인증키

    역시 공식문서를 잘읽어야 한다.( https://docs.microsoft.com/en-us/azure/postgresql/flexible-server/quickstart-create-connect-server-vnet)

    내가 놓치고 있던 부분은 azure postgres 서버를 사용할 app 서버는 postgresSQL client tool을 설치해야한다는 점이였다.

    sudo apt-get install postgresql-client

    보안을 위해 SSL 인증서를 다운 받아서 사용한다.

    wget --no-check-certificate https://dl.cacerts.digicert.com/DigiCertGlobalRootCA.crt.pem

    이제 app서버에서 접속해도 잘된다!!

    psql --host=mydemoserver-pg.postgres.database.azure.com --port=5432 --username=myadmin --dbname=postgres --set=sslmode=require --set=sslrootcert=DigiCertGlobalRootCA.crt.pem

TypeORM 에서 azure postgres server 연결하기

psql --host=mydemoserver-pg.postgres.database.azure.com --port=5432 --username=myadmin --dbname=postgres --set=sslmode=require --set=sslrootcert=DigiCertGlobalRootCA.crt.pem

bash에서 위와 같이 ssl mode로 인증서를 가지고 postgres server에 접속을 한다. 그렇다면 typeorm을 사용해서 postgres server에 연결할때, 이와 같은 과정을 어떻게 구현할까?

TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        type: 'postgres',
        host: configService.get<string>('POSTGRES_HOST'),
        port: 5432,
        username: configService.get<string>('POSTGRES_USER'),
        password: configService.get<string>('POSTGRES_PASSWORD'),
        database: configService.get<string>('POSTGRES_DB'),
        entities: ['dist/**/*.entity{.d.ts,.js}', '**/*.entity{.d.ts,.js}'],
        synchronize: configService.get<boolean>('POSTGRES_SYNC'),
        ssl:
          process.env.NODE_ENV === 'production'
            ? {
                cert: readFileSync(
                  '/home/azureuser/DigiCertGlobalRootCA.crt.pem',
                ),
              }
            : false,
      }),
    }),

ssl 부분을 보면된다. ssl mode는 false일때는 작동하지않지만, 위와같이 객체에 cert를 키로하는 pem키를 읽어서 보내면 잘 작동한다.

출처: https://node-postgres.com/features/ssl

출처: https://stackoverflow.com/questions/56660312/cannot-connect-an-ssl-secured-database-to-typeorm

좋은 웹페이지 즐겨찾기