TypeORM 没有成功连接到我的 Postgres

TypeORM does not connect successfully to my Postgres

我正在为一个小型网站使用 Express、Postgres 和 TypeORM。

我在将 TypeORM 与我的 Postgres 连接时遇到问题。

index.ts

( async ()=>{
    console.log("before")  <-- This gets printed
    await createConnection()
    console.log("after") <-- This does NOT get printed
    app.listen(4000, ()=>{
        console.log('express server is listening on port 4000')
    })

})()

ormconfig.json

{
   "type": "postgres",
   "host": "localhost",
   "port": 5432,
   "username": "postgres",
   "password": "postgres",
   "database": "jwtauthexample",
   "synchronize": true,
   "logging": true,
   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
   }
}

我创建了一个 postgres 数据库和用户如下

  1. brew install postgres
  2. psql postgres 在我的终端上
  3. CREATE DATABASE jwtauthexample
  4. CREATE USER postgres WITH ENCRYPTED PASSWORD postgres
  5. GRANT ALL PRIVILEGES ON DATABASE jwtauthexample TO postgres

我不确定我做错了什么。

同样的 index.ts 代码几周前就可以工作了。然后我删除了 postgres,它无法再使用我一直使用的相同程序连接。

我很确定问题出在 await createConnection(),因为一旦我 运行,它就不会执行行 console.log("before)yarn start(使用 nodemon)

已解决

我忘了 运行: npm install pg --save 在我的应用程序中

如果您使用纱线,请执行以下操作: yarn add pg

我之前也遇到过这个问题,貌似是pg的问题,升级pg版本后就可以了,请按如下操作

1. npm uninstall pg --save

2。 npm 安装 pg --save

我是 运行 版本 6.4.2,现在它将安装最新版本(对我来说是 8.7.1)