恐慌:无法连接:拨号 TCP X.X.X.X:5432:连接:连接被拒绝

Panic : Could not connect : Dial TCP X.X.X.X:5432 : Connect : Connection refused

我正在尝试使用 Docker Compose 安装 TimeScaleDB,但在使用 timescaledb-parallel-copy 导入数据时出现以下错误:

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/import_data.sh timescaledb | panic: could not connect: dial tcp 172.18.0.2:5432: connect: connection refused timescaledb | timescaledb | goroutine 6 [running]: timescaledb | main.processBatches(0xc000016730, 0xc000060780) timescaledb | /go/src/github.com/timescale/timescaledb-parallel-copy/cmd/timescaledb-parallel-copy/main.go:238 +0x8bb timescaledb | created by main.main timescaledb | /go/src/github.com/timescale/timescaledb-parallel-copy/cmd/timescaledb-parallel-copy/main.go:148 +0x1d2 timescaledb | panic: could not connect: dial tcp 172.18.0.2:5432: connect: connection refused

这是我的 docker 撰写和我的 docker 文件:

Docker 撰写:

version: "3.8"
services:
  timescaledb:
    container_name: timescaledb
    build:
      context: "./timescaledb"
      dockerfile: "docker_file"
    env_file:
      - "./timescaledb/environment.env"
    volumes:
      - "./timescaledb/data:/data"
    ports:
      - "5432:5432/tcp"
    networks:
      - local_network
    restart: on-failure
networks:
  local_network:

Docker 文件:

FROM timescale/timescaledb:latest-pg13
ADD create_tables.sql /docker-entrypoint-initdb.d
ADD import_data.sh /docker-entrypoint-initdb.d
RUN chmod a+r /docker-entrypoint-initdb.d/*

这是调用 timescaledb-parallel-copy 的 import_data.sh 脚本:

#!/bin/bash
timescaledb-parallel-copy --connection "host=timescaledb user=postgres password=XXX sslmode=disable" --db-name YYY --table ZZZ --copy-options "CSV" -skip-header --columns "name, unit" --file "/data/data.csv" --reporting-period 30s workers 4

我也试过使用本地主机,但我得到了同样的错误。

编辑:问题已解决。不应在连接字符串中指定主机。根据官方文档(https://hub.docker.com/_/postgres):

Also, as of docker-library/postgres#440, the temporary daemon started for these initialization scripts listens only on the Unix socket, so any psql usage should drop the hostname portion (see docker-library/postgres#474 (comment) for example).