markdown psql如何使用SSH隧道远程服务器中的数据库?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown psql如何使用SSH隧道远程服务器中的数据库?相关的知识,希望对你有一定的参考价值。


### Format

#### First terminal (dev machine):

```
$ ssh -nNT -L local-port:127.0.0.1:remote-port remote-db-user@remote-ip
```

#### Second terminal (dev machine):

```
$ psql -h 127.0.0.1 -p 9000 -U remote-db-user -d db-name
```

### Example

Assuming the remote host IP address is `12.34.567.890` and we want to connect to Postgres running inside it on port `5432`. We can SSH tunnel into it from our dev machine on port `9000`.

Assuming the database user/role is `john` and the database that we want to connect to is called  `blog`.

#### First terminal (dev machine):

```
$ ssh -nNT -L 9000:127.0.0.1:5432 john@12.34.567.890
```

#### Second terminal (dev machine):

```
$ psql -h 127.0.0.1 -p 9000 -U john -d blog
```

### Notes

1. The above command will prompt for `john` password. See reference link below to learn how to set password for database user/role.

2. Postgres is using 'ident authentication' which means the database user/role is also a Linux user. In this case, the Linux user is `john` and the database user/role is also `john`. See reference link below to learn how to add user.

### References:

- [http://bit.ly/2BVQ8DH](http://bit.ly/2BVQ8DH)
- [http://bit.ly/2Nsa3eC](http://bit.ly/2Nsa3eC)
- [http://bit.ly/2NrWTys](http://bit.ly/2NrWTys)
- [http://bit.ly/2PMFfae](http://bit.ly/2PMFfae)
- [http://bit.ly/2BUnqmy](http://bit.ly/2BUnqmy)

以上是关于markdown psql如何使用SSH隧道远程服务器中的数据库?的主要内容,如果未能解决你的问题,请参考以下文章

通过 SSH 隧道在 PostgreSQL 上进行身份验证失败

如何使用 Fabric 通过 2 个网关将 SSH 隧道连接到远程主机?

如何配置系统通过 ssh 反向隧道/代理使用 git?

jmx/jstatd 通过 ssh 隧道访问远程机器

如何在两跳 SSH 隧道上使用 JProfiler

使用 SSH 隧道的 PostgreSQL/psycopg2 密码验证