postgresql - 通过ssh隧道连接数据库

标签 postgresql shell ssh proxy ssh-tunnel

我们的生产数据库只能从生产应用程序服务器访问。我能够登录到生产应用程序服务器并使用 psql 登录到数据库,但我想设置一个 ssh 隧道以允许我从工作箱访问生产数据库。

理想情况下,这将是我可以从工作箱运行的单个命令,用于在生产应用程序服务器上设置隧道/代理

这是我想出来的,但它不起作用。

user@workbox $ ssh -fNT -L 55555:db.projectX.company.com:5432 app.projectX.company.com
user@workbox $ psql -h app.projectX.company.com -p 55555

psql: could not connect to server: No route to host
Is the server running on host "app.projectX.company.com" (10.1.1.55) and accepting
TCP/IP connections on port 55555?

报告的 IP 地址不正确。

最佳答案

连接到隧道端点时,主机名是您的本地主机,因为这是转发端口公开的地方。

ssh -fNT -L 55555:db.projectX.company.com:5432 app.projectX.company.com
psql -h localhost -p 55555

顺便说一句,PgAdmin-III 提供 ssh 隧道自动化。另一方面,它是一个大型 GUI 应用程序,没有 psql 方便的 \commands

编写一个 sshpsql bash 脚本来启动 ssh 隧道、存储 ssh 进程的 pid、启动 psql、让你做你想做的事情,并在退出时杀死 ssh 是非常简单的隧道。您还需要TRAP“kill $sshpid”EXIT,以便在不干净的导出处杀死隧道。

关于postgresql - 通过ssh隧道连接数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23966349/

相关文章:

linux - 在与符号链接(symbolic link)同名的目录中提取符号链接(symbolic link) zip 文件的内容

shell - 在命令行上 merge Mercurial 冲突?

java - 如何在 String 中获取 jsch shell 命令输出

postgresql - 如何用空值替换非ascii字符

sql - Postgres : What is the query 'select * from user' actually doing?

Ruby-on-rails Postgres 查询

node.js - 如何在 Sequelize 中正确配置和计算belongsToMany 关联?

bash - 使用脚本在 shell 中创建命令循环

linux - 如何使 shell 脚本自动化并读取当前时间?

ssh - 将公钥发送到远程主机的命令