Why go through SSH at all
MySQL listens on port 3306 and PostgreSQL on 5432. On a well-kept server those ports are bound to 127.0.0.1 or a private network and closed in the firewall, so nothing outside can reach them. SSH is already open, already encrypted and already tied to your keys, so it carries the database connection too. No extra port is opened and no database password crosses the internet in the clear.
The manual way: an SSH tunnel
One command forwards a port on your computer to the database on the server. Leave it running, then connect your SQL client to your own end of it.
- MySQL or MariaDB: run ssh -N -L 3307:127.0.0.1:3306 you@server, then connect to 127.0.0.1 on port 3307.
- PostgreSQL: run ssh -N -L 5433:127.0.0.1:5432 you@server, then connect to 127.0.0.1 on port 5433.
- Behind a bastion: add -J you@bastion and SSH hops through it first.
- A database on another machine in the private network: put its private address, as the server sees it, in place of 127.0.0.1.
- The first number is the local port and is yours to choose. One above the real port avoids a clash with a database running on your own machine.
Where it goes wrong
- The tunnel dies when the laptop sleeps or the network changes, and the SQL client reports a lost connection without saying why.
- Two projects want the same local port, or a local database already has it.
- A MySQL client told to connect to "localhost" uses a socket file instead of the network and never enters the tunnel. Use 127.0.0.1.
- Every teammate keeps their own list of commands, ports and which key belongs to which server.
BranchShell: the tunnel is part of the connection
BranchShell is an SSH client first. Your hosts live in an encrypted vault on your computer with their keys, users and jump hosts. A database connection in the same vault can name one of those hosts: when you open it, BranchShell opens a forward on that host and connects through it. There is no command to keep running in another window.
- Eight SQL engines in the table grid and the query console: MySQL, MariaDB, PostgreSQL, SQLite, SQL Server, ClickHouse, Redshift and Turso.
- The same keys, identities and jump host chains your terminal sessions use, so nothing is set up twice.
- Credentials kept in the keychain, in a vault that stays on your machine, with no account and no cloud sync.
- A terminal and SFTP on the same host, one tab away, for when the query is not enough.
What the SQL client does once you are in
- A table grid with a WHERE bar and an ORDER BY bar, paging, and export as CSV, JSON or SQL.
- A query console with autocomplete that runs the statement under the cursor, a selection or several, each with its own result tab.
- Cells edited in the grid and submitted together. Modify table edits columns and indexes and shows the SQL it will run.
- Full-text search through every column of every table.
Pay once, run it on 2 computers
BranchShell for macOS, Windows and Linux costs $20 once for 2 computers. There is no account to create and no subscription to renew, and the trial lasts 14 days with every feature.