I have several databases hosted outside the application server, ex: DB instances:


now I want to forward the external dbs to ports 5431-5433 and be able to connect with psql to localhost:

psql -h -p 5432 -U <user> -d <db>

I get:

psql: could not connect to server: Connection refused
        Is the server running on host "" and accepting
        TCP/IP connections on port 5432?

services on this server are only allowed to connect to localhost, additionally these ports will be forwarded with ssh.

I tried firewalld masquerade:

firewall-cmd --zone=internal --add-masquerade --permanent
firewall-cmd --zone=internal --add-forward-port=port=5432:proto=tcp:toport=5432:toaddr= --permanent
firewall-cmd --reload

Any ideas? (I've cowered several manuals, including CentOS, Fedora and Redhat intros)

EDIT: Only Postgres Ports are open on the respective servers, nothing else. I will try using a postgresql aware gateway like crunchy-proxy

1 Answer 1


It depends how is the postgresql pg_hba.conf configured and whether the postgresql server listen on IP addresses you've listed.

Otherwise If You can ssh to those servers You can use ssh port forwarding -L option in ssh(1) example:

ssh [email protected] -L 5431:localhost:5432 -f
ssh [email protected] -L 5432:localhost:5432 -f
ssh [email protected] -L 5433:localhost:5432 -f

You can achieve the same with following ~/.ssh/config

Host db1
        LocalForward            localhost:5431 localhost:5432

Host db2
        LocalForward            localhost:5432 localhost:5432

Host db3
        LocalForward            localhost:5433 localhost:5432

and then just

ssh -f db1
ssh -f db2
ssh -f db3
  • Only Postgres Ports are open on the respective servers, nothing else. (For "security" reasons...). Feb 22, 2019 at 10:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .