I run these two commands all the time to connect to my rds instance on aws that's protected behind a firewall (so i tunnel through the ec2 instance) like so:
command 1: open the tunnel (run on background)
ssh -N -L port:host:5432 user@$ip -i ~/.ssh/key.pub & command 2: connect to db through tunnel port:
PGPASSWORD=password psql dbname -U user -h ip_address -p port; which is awesome, but I would like to put both these in a single function. But nothing worked with me:
attempt 1: run without background stuff:
function db() { ssh -N -L port:host:5432 user@$ip -i ~/.ssh/key.pub & PGPASSWORD=password psql dbname -U user -h ip_address -p port; } simply tells me this:
$proddb [1] 62924 psql: could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 6666? although the initial command is running in the background:
ps aux | grep host (standard input):435:abdullah 62924 0.0 0.0 4315660 5828 s006 S 3:06PM 0:00.03 ssh -N -L port:host:5432 user@$ip -i ~/.ssh/key.pub and if i immediately run the next command after it.. i connect to the db just fine!
PGPASSWORD=password psql dbname -U user -h ip_address -p port; user=> how do I make this work?