我在试着连接
访问托管在远程服务器上的PostgreSQL数据库。
在我的笔记本电脑上启动的PostGIS 码头集装箱 (Ubuntu18.04)。
我的第一步是自然地从我的笔记本电脑打开一个SSH隧道到远程服务器。到目前为止,一切都没问题,因为我可以通过SSH隧道连接到远程服务器上的终端。
然后,基于这个答案:https://stackoverflow.com/a/24326540/6630397,我在我的Ubuntu18.04机器上启动了PostGIS停靠容器,以连接到远程PostgreSQL数据库。(这对我来说是必要的,因为我的本地pg_tools和远程服务器上的pg版本之间存在一些版本不匹配的问题。)
但是,当试图从本地容器连接到远程数据库时,连接超时。
技术细节
让我们假设远程服务器上的PostgreSQL端口是默认的5432,SSH隧道被映射到本地5433,如下所示:
ssh -f -N <user@remote-server> -L 5433:127.0.0.1:5432PostGIS码头容器启动时为:
$ docker run --rm -d \
--add-host host.docker.internal:host-gateway \
--name postgis \
-v "/path/to/local/data_folder:/data" \
-e POSTGRES_DB=mydbname \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=password \
postgis/postgis:13-3.1然后,我简单地使用以下方法连接到它:
$ docker exec -it postgis bash(通过指定-u postgres连接到这个postgis容器并不能解决这个问题)
从容器内部:
root@c1246180b316:/# psql -U postgres -h host.docker.internal -p 5433但是,连接实际上是超时的(它应该在那里请求远程db密码):
psql: error: could not connect to server: Connection timed out
Is the server running on host "host.docker.internal" (172.17.0.1) and accepting
TCP/IP connections on port 5433?如果我从码头容器外部运行完全相同的psql命令,直接在本地机器上运行,用localhost替换host.docker.internal,它工作得很好!
如何解决此问题并成功地从postgis容器中连接到远程数据库?
发布于 2022-03-08 23:00:42
我的解决方案是确保隧道接受所有传入的连接。出于某种原因,对接者似乎不算来自机器或类似的东西。
所以,把隧道改为:
ssh -f -N <user@remote-server> -L 0.0.0.0:5433:127.0.0.1:5432https://stackoverflow.com/questions/68214643
复制相似问题