Sequelize @ 6.3.4无法连接到docker-> postgres

时间:2020-08-07 06:37:13

标签: node.js postgresql docker connection sequelize.js

enter image description here

Nodejs:

import * as Sequelize from 'sequelize'

const fn =async ()=>{

  const sequelize = new Sequelize.Sequelize('postgres://a:a@localhost:5432/a')

  console.log('1');
  try {
    // setInterval(()=> console.log(`>>`), 1000)
    const a = await sequelize.authenticate();
    console.log('Connection has been established successfully.', a);
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }
  console.log(`>>>ss`)
}

fn()

仅注销:

1
Done in 2.41s.

哪个很奇怪。它关闭了我的节点服务器,没有错误,只是关闭了。


所以我在以下端口尝试了端口5432:

  • 本地主机
  • 192.168.0.1
  • 192.168.65.0
  • 0.0.0.0(docker ps显示0.0.0.0:5432->5432/tcp

但是没有工作的机会。您如何连接到数据库?我一直在Windows上遇到有关docker的问题,似乎设置在与我的浏览器和node.js进程无法访问的网络不同的网络上。

enter image description here

如果使用管理员,则可以访问数据库。 (我可以登录并查看数据库) enter image description here

dockercompose:

version: "3.7"
services:
  db:
    restart: always
    image: postgres:12.3-alpine
    environment:
      POSTGRES_DB: a
      POSTGRES_USER: a
      POSTGRES_PASSWORD: a
    ports:
      - "5432:5432"

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

Pg管理员有效*

Docker检查:

"PortBindings": {
                "5432/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "5432"
                    }
                ]
            },

你们知道该怎么办吗?

1 个答案:

答案 0 :(得分:0)

似乎您需要运行yarn add pg pg-hstore才能正常工作。