TypeORM PostgreSQL can't connect

3.1k views Asked by At

I can't get my database and server to connect to each other. The server is express and the database is made with a docker image from postgress.

I tried

  • deleting node_modules
  • restarting postgress container
  • npm install pg --save

error

[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)

Package.json

 "pg": "^8.7.1"

class class 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));
}

}

docker-compose

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:
1

There are 1 answers

0
Alex Skotner On

When connecting 2 elements in docker-compose you don't provide "localhost" as the host, you need to provide the name of the service to create a successful connection. So the correct way to connect the server to the database, in this case, would be to specify the service name as the host in the ConnectionOptions:

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