What is a tunnel?
A tunnel specifies a given port on the local (client) host that is to be forwarded to the given host and port on the remote side. How it works? This works by allocating a socket to listen to a port on the local side, optionally bound to the specified bind_address. Whenever a connection is made to this port, the connection is forwarded over the secure channel, and a connection is made to the port from the remote machine.
Making the tunnel
Before type the command line to create the ssh tunnel I’m assuming the following topology

If I would like to connect to 192.168.0.2 db server and the database is a postgresql, then the default port is 5432 and the tunnel command line is:
$ ssh -L 5432:192.168.0.2:5432 <user>@<web server>
_____^______ ______^______
/ local port \ / remote port \
If you are running a postgresql in your own machine you can chose another port to listen in your loop back. I always have a pgsql running in my laptop because I use it in most of my developments and I use port 5435.
$ ssh -L 5435:192.168.0.2:5432 <user>@<web server>
============================================================
from other terminal
============================================================
$ psql -h localhost -p 5435 -U db_user -d db_name
Welcome to psql 7.3.4, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
db_bame=#
Read more: easytech blog
QR: