SSH 隧道到 postgres 数据库

hob*_*es3 3 postgresql ssh ssh-tunnel psql

目前我在远程开发服务器上开发。我通过 SSH 连接到开发服务器(使用 SSH 密钥)作为

ssh -p 22222 user@devbox.com
Run Code Online (Sandbox Code Playgroud)

然后从开发服务器,我通过本地网络连接到数据库

psql -U postgres -h psqldb -d my_database
Run Code Online (Sandbox Code Playgroud)

wherepsqldb指向10.0.0.202开发服务器的/etc/hosts文件。

我想在我自己的电脑上开始本地开发。如何psqldb通过使用远程开发服务器作为 SSH 隧道从本地机器连接到Postgres 服务器(或者有更好的方法来做到这一点)?

编辑

我没有提到我已经在本地服务器上安装了 Postgres 并在默认端口 5432 上监听。

Tom*_*zky 7

ssh -L localhost:5432:psqldb:5432 -p 22222 user@devbox.com
Run Code Online (Sandbox Code Playgroud)

在另一个终端上:

psql -h localhost -d my_database -U postgres
Run Code Online (Sandbox Code Playgroud)

简单、安全和加密。我不知道更好的方法。