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

SSH隧道代理PostgreSQL连接时psql长时间卡顿后才成功连接的问题求助

SSH隧道代理PostgreSQL连接时psql长时间卡顿后才成功连接的问题求助

大家好,我碰到了一个很费解的问题,想请教下各位:

我现在通过SSH隧道代理连接PostgreSQL服务器,拓扑结构是这样的:
[M2 MacBook笔记本] ---(SSH)--> [任意Linux中转服务器] --(TCP 5432)--> [PostgreSQL服务器]

我用这条命令建立SSH隧道:

ssh user@arbitrarylinuxserver -L5432:postgres:5432

问题现象

  • 当我在本地笔记本上执行 psql -h 127.0.0.1 时,连接会卡顿至少好几分钟,之后才会弹出「Username:」的输入提示
  • 直接在Linux中转服务器上执行 psql -h postgresserver.example.com 可以立即连接,完全没有延迟
  • SSH本身的连接非常顺畅,在中转服务器上输入命令没有任何卡顿
  • 本地的psql在不使用SSH隧道时工作完全正常,且PostgreSQL服务器因防火墙限制只能通过这个隧道访问

我给SSH命令加上-vvvv参数后,在尝试从本地连接PostgreSQL时,SSH窗口输出了以下日志(卡顿期间和恢复后的内容):

debug1: Connection to port 5432 forwarding to postgres port 5432 requested.
debug2: fd 12 setting TCP_NODELAY
debug3: fd 12 is O_NONBLOCK
debug3: fd 12 is O_NONBLOCK
debug1: channel 3: new [direct-tcpip]
debug3: send packet: type 90
debug3: receive packet: type 91
debug2: channel 3: open confirm rwindow 2097152 rmax 32768
[... hangs for a while ...]
debug3: receive packet: type 96
debug2: channel 3: rcvd eof
debug2: channel 3: output open -> drain
debug2: channel 3: obuf empty
debug2: chan_shutdown_write: channel 3: (i0 o1 sock 12 wfd 12 efd -1 [closed])
debug2: channel 3: output drain -> closed
debug2: channel 3: read failed rfd 12 maxlen 32768: Broken pipe
debug2: channel 3: read failed
debug2: chan_shutdown_read: channel 3: (i0 o3 sock 12 wfd 12 efd -1 [closed])
debug2: channel 3: input open -> drain
debug2: channel 3: ibuf empty
debug2: channel 3: send eof
debug3: send packet: type 96
debug2: channel 3: input drain -> closed
debug2: channel 3: send close
debug3: send packet: type 97
debug3: channel 3: will not send data after close
debug1: Connection to port 5432 forwarding to postgres port 5432 requested.
debug2: fd 13 setting TCP_NODELAY
debug3: fd 13 is O_NONBLOCK
debug3: fd 13 is O_NONBLOCK
debug1: channel 4: new [direct-tcpip]
debug3: send packet: type 90
debug3: channel 3: will not send data after close
debug3: channel 3: will not send data after close
debug3: receive packet: type 97
debug2: channel 3: rcvd close
debug3: channel 3: will not send data after close
debug2: channel 3: is dead
debug2: channel 3: garbage collecting
debug1: channel 3: free: direct-tcpip: listening port 5432 for postgres port 5432, connect from 127.0.0.1 port 56662 to 127.0.0.1 port 5432, nchannels 5
debug3: channel 3: status: The following connections are open:
#2 client-session (t4 r0 i0/0 o0/0 e[write]/0 fd 9/10/11 sock -1 cc -1 io 0x01/0x00)
#3 direct-tcpip: listening port 5432 for postgres port 5432, connect from 127.0.0.1 port 56662 to 127.0.0.1 port 5432 (t4 r1 i3/0 o3/0 e[closed]/0 fd 12/12/-1 sock 12 cc -1 io 0x00/0x00)
#4 direct-tcpip: listening port 5432 for postgres port 5432, connect from 127.0.0.1 port 56668 to 127.0.0.1 port 5432 (t3 nr0 i0/0 o0/0 e[closed]/0 fd 13/13/-1 sock 13 cc -1 io 0x00/0x00)
debug3: receive packet: type 91
debug2: channel 4: open confirm rwindow 2097152 rmax 32768
debug2: channel 4: read failed rfd 13 maxlen 32768: Broken pipe
debug2: channel 4: read failed
debug2: chan_shutdown_read: channel 4: (i0 o0 sock 13 wfd 13 efd -1 [closed])
debug2: channel 4: input open -> drain
debug2: channel 4: ibuf empty
debug2: channel 4: send eof
debug3: send packet: type 96
debug2: channel 4: input drain -> closed
debug3: receive packet: type 96
debug2: channel 4: rcvd eof
debug2: channel 4: output open -> drain
debug3: receive packet: type 97
debug2: channel 4: rcvd close
debug3: channel 4: will not send data after close
debug3: channel 4: will not send data after close
debug2: channel 4: obuf empty
debug2: chan_shutdown_write: channel 4: (i3 o1 sock 13 wfd 13 efd -1 [closed])
debug2: chan_shutdown_write: channel 4: shutdown() failed for fd 13 [i3 o1]: Socket is not connected
debug2: channel 4: output drain -> closed
debug2: channel 4: send close
debug3: send packet: type 97
debug2: channel 4: is dead
debug2: channel 4: garbage collecting
debug1: channel 4: free: direct-tcpip: listening port 5432 for postgres port 5432, connect from 127.0.0.1 port 56668 to 127.0.0.1 port 5432, nchannels 4
debug3: channel 4: status: The following connections are open:
#2 client-session (t4 r0 i0/0 o0/0 e[write]/0 fd 9/10/11 sock -1 cc -1 io 0x01/0x00)
#4 direct-tcpip: listening port 5432 for postgres port 5432, connect from 127.0.0.1 port 56668 to 127.0.0.1 port 5432 (t4 r1 i3/0 o3/0 e[closed]/0 fd 13/13/-1 sock 13 cc -1 io 0x00/0x02)
[now my postgres connection is working in the other terminal]

我之前使用SSH隧道从来没遇到过这种先卡顿很久才正常工作的情况,请问有没有办法解决这个问题?

备注:内容来源于stack exchange,提问作者Ian Ling

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.22 08:58:14