【问题标题】:Sequelize @6.3.4 can't connect to docker -> postgresSequelize @6.3.4 无法连接到 docker -> postgres
【发布时间】:2020-11-27 11:28:37
【问题描述】:

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 进程可以访问的网络不同的网络上。

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

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"
                    }
                ]
            },

你们知道该怎么做吗?

【问题讨论】:

  • 您的屏幕截图没有显示 pgAdmin 无法连接。它只是表明你没有在 pgAdmin 中注册任何服务器。
  • 如何注册?通常它会显示一个加号按钮,但现在没有。具有正确连接字符串的 node.js 也无法连接
  • IIRC(我当前的机器上没有安装它),右键单击 Servers 节点。
  • 谢谢,好像它正在使用 postgres。我猜现在仍然是续集的问题。

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


【解决方案1】:

看来您需要运行yarn add pg pg-hstore 才能工作。

【讨论】:

    猜你喜欢
    • 2020-08-26
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多