Instead of doing the shell dance, I propose to use a local forwarding.
You can forward the database socket to your ssh client and talk to the database directly.
You can forward the database socket to your ssh client and talk to the database directly.