TypeORM PostgreSQL 无法连接

Ale*_*ner 7 postgresql express typeorm typeorm-datamapper node.js-typeorm

我无法让我的数据库和服务器相互连接。服务器是express的,数据库是用postgress的docker镜像制作的。

我试过

  • 删除node_modules
  • 重启 postgress 容器
  • npm 安装 pg --save

错误

[2021-11-01T00:48:05.774Z] INFO: Express server started on port: 3000
DriverPackageNotInstalledError: Postgres package has not been found installed. Try to install it: npm install pg --save
    at DriverPackageNotInstalledError.TypeORMError [as constructor] (/Users/alexskotner/Documents/GitHub/Aware/BackEnd/src/error/TypeORMError.ts:7:9)
    at new DriverPackageNotInstalledError (/Users/alexskotner/Documents/GitHub/Aware/BackEnd/src/error/DriverPackageNotInstalledError.ts:8:9)
    at PostgresDriver.loadDependencies (/Users/alexskotner/Documents/GitHub/Aware/BackEnd/src/driver/postgres/PostgresDriver.ts:1118:19)
    at new PostgresDriver (/Users/alexskotner/Documents/GitHub/Aware/BackEnd/src/driver/postgres/PostgresDriver.ts:284:14)
    at DriverFactory.create (/Users/alexskotner/Documents/GitHub/Aware/BackEnd/src/driver/DriverFactory.ts:36:24)
    at new Connection (/Users/alexskotner/Documents/GitHub/Aware/BackEnd/src/connection/Connection.ts:122:43)
    at ConnectionManager.create (/Users/alexskotner/Documents/GitHub/Aware/BackEnd/src/connection/ConnectionManager.ts:61:28)
    at /Users/alexskotner/Documents/GitHub/Aware/BackEnd/src/globals.ts:77:35
    at step (/Users/alexskotner/Documents/GitHub/Aware/BackEnd/node_modules/typeorm/node_modules/tslib/tslib.js:143:27)
    at Object.next (/Users/alexskotner/Documents/GitHub/Aware/BackEnd/node_modules/typeorm/node_modules/tslib/tslib.js:124:57)
Run Code Online (Sandbox Code Playgroud)

包.json

 "pg": "^8.7.1"
Run Code Online (Sandbox Code Playgroud)

类 DatabaseAPI {

private opt: ConnectionOptions = {
    type: 'postgres',
    host: "localhost",
    port: 5432,
    username: "me",
    password: "password",
    database: "alex",
    entities: [
        User 
    ],
    synchronize: true,
    logging: false
}

constructor() {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-call
    createConnection(this.opt).then((connection: Connection) => {
        // here you can start to work with your entities
        const c = new Company('Aware', '0001', '')

        return connection.manager
            .save(c)
            .then(c => {
                console.log("cimpany has been saved. comp name is", c.name);
            });

    }).catch(error => console.log(error));
}
Run Code Online (Sandbox Code Playgroud)

}

docker 撰写

version: '3.1'

services:

  db:
    image: postgres:14.0
    container_name: postfres
    volumes: 
     - db-data:/var/lib/postgresql/data
    ports:
        - 5432:5432
    environment:
        POSTGRES_USER: ${POSTGRES_USER}
        POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
        PGDATA: /var/lib/postgresql/data/

  adminer:
    image: adminer
    ports:
      - 8080:8080

volumes:
  db-data:
Run Code Online (Sandbox Code Playgroud)

Ale*_*ner 2

在 docker-compose 中连接 2 个元素时,您不提供“localhost”作为主机,您需要提供服务的名称才能创建成功的连接。因此,在这种情况下,将服务器连接到数据库的正确方法是在 ConnectionOptions 中将服务名称指定为主机:

 private opt: ConnectionOptions = {
    type: 'postgres',
    host: "db",
    port: 5432,
    username: "me",
    password: "password",
    database: "alex",
    entities: [
        User 
    ],
    synchronize: true,
    logging: false
}
Run Code Online (Sandbox Code Playgroud)