You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法连接本地Postgres Docker容器问题排查与解决

解决方案

问题根源是我的VPN禁止了所有内部网络连接,之前没意识到会出现这种情况。

需求描述

我希望在Ubuntu系统中通过Docker容器运行Postgres,然后直接在Ubuntu系统上使用psql连接该容器——注意:我不想进入容器内部再执行psql,而是要从宿主系统直接连接运行中的容器。

已尝试操作

我执行了以下命令:

docker run --rm -d \
        -e POSTGRES_PASSWORD=password \
        -e POSTGRES_DB=example \
        -e POSTGRES_USER=user \
        -p 5432:5432 postgres:14.6-bullseye

得到输出:

> docker ps 
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS        PORTS                                       NAMES
bd655ef0830a   postgres:14.6-bullseye   "docker-entrypoint.s…"   3 seconds ago   Up 1 second   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   tender_ardinghelli

之后我尝试从宿主系统(而非容器内部)用以下psql命令连接:

psql -h localhost --port 5432 --dbname example -U user

出现错误:

psql: error: connection to server at "localhost" (::1), port 5432 failed: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

我不清楚问题出在哪——已经明确指定了连接端口,而且自认为docker run命令中的端口映射设置正确。

后续尝试

尝试修改docker run中的端口

“如果宿主系统已运行Postgres,建议通过转发到非5432端口来避免冲突”

我修改了docker run命令:

docker run --name example \
    --rm -d \
    -e POSTGRES_PASSWORD=password \
    -e POSTGRES_DB=db -e POSTGRES_USER=user \
    -p 5499:5432 \
    postgres:14.6-bullseye

将宿主端口从5432改为5499,避免与宿主系统中已运行的Postgres冲突。

执行命令后,docker ps输出:

$ docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS        PORTS                                       NAMES
4172d2d12bb8   postgres:14.6-bullseye   "docker-entrypoint.s…"   2 seconds ago   Up 1 second   0.0.0.0:5499->5432/tcp, :::5499->5432/tcp   example

尝试从宿主系统用psql连接:

$ psql -h localhost --port 5499 --dbname db -U user
psql: error: connection to server at "localhost" (::1), port 5499 failed: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

依然出现相同错误:S

尝试修改postgres.conf中的listen_addresses

我更新了/etc/postgresql/14/main/postgresql.conf文件,添加配置:

listen_addresses = '*'      # what IP address(es) to listen on;
                    # comma-separated list of addresses;
                    # defaults to 'localhost'; use '*' for all
                    # (change requires restart)

并通过以下命令重启Postgres:

sudo systemctl restart postgresql

但仍然出现上述相同错误。

系统信息

Ubuntu系统:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.1 LTS
Release:        22.04
Codename:       jammy

Docker版本:

Docker version 20.10.22, build 3a2c30b

宿主系统psql版本:

$ psql --version 
psql (PostgreSQL) 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1)

在宿主系统执行systemctl status postgresql的输出:

● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Sat 2022-12-31 02:18:33 GMT; 1min 0s ago
    Process: 17727 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 17727 (code=exited, status=0/SUCCESS)
        CPU: 1ms

在/etc目录下查找包含post的目录:

$ /etc> find . -maxdepth 1 -type d | sort | grep post
./postgresql
./postgresql-common

内容的提问来源于stack exchange,提问作者baxx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 05:35:35