How to Access a Remote PostgreSQL Database over SSH
Your application runs on a Linux VPS, and its PostgreSQL database is only accessible from the server. Now you need to inspect a table, investigate a bug, or verify a migration from your computer.
Opening port 5432 to the internet might seem like the easiest solution. However, an SSH tunnel lets you connect to PostgreSQL without making the database publicly accessible.
This guide explains how to access PostgreSQL over SSH using the command line, DBeaver, and an alternative server management workflow with Server Explorer.
Why access PostgreSQL over SSH?
PostgreSQL commonly listens on port 5432. For applications running on the same server, the database can listen only on the loopback interface, keeping it inaccessible directly from the internet.
SSH local port forwarding creates a connection between a port on your computer and a database endpoint reachable from your SSH server. Traffic between your computer and the SSH server travels through an encrypted channel.
For example:
- Local endpoint:
127.0.0.1:15432 - SSH server:
your-server-ip:22 - Remote PostgreSQL endpoint:
127.0.0.1:5432
Your database client connects to local port 15432. SSH forwards the traffic to PostgreSQL on the VPS. Database authentication still applies: an SSH account does not automatically grant access to database tables.
1. Check your PostgreSQL installation
Before creating a tunnel, connect to your VPS:
ssh deploy@your-server-ip
On a typical systemd-based installation, check the service and listening port:
sudo systemctl status postgresql
sudo ss -ltnp | grep ':5432'
The service name may differ depending on your distribution. A listener on 127.0.0.1:5432 is sufficient for the tunnel in this example.
If PostgreSQL accepts local socket connections and you have administrative access, inspect its settings:
sudo -u postgres psql -c 'SHOW listen_addresses;'
sudo -u postgres psql -c 'SHOW port;'
You also need a database name, an authorized database user, and its authentication credentials. If SSH itself fails, see this guide to fixing SSH public-key authentication errors.
2. Connect to PostgreSQL using an SSH tunnel
Open a terminal on your computer, not inside your existing remote SSH session. With OpenSSH installed, run:
ssh -N -L 127.0.0.1:15432:127.0.0.1:5432 -o ExitOnForwardFailure=yes deploy@your-server-ip
Here is what the options mean:
| Option | Purpose |
|---|---|
-N | Establish a tunnel without running a remote shell |
-L | Forward a local port to a remote destination |
127.0.0.1:15432 | Listen only on your computer’s loopback interface |
127.0.0.1:5432 | Reach PostgreSQL from the SSH server |
ExitOnForwardFailure=yes | Exit if the forwarding listener cannot be established |
Keep this terminal open. If the command produces no output after authentication, that is normally expected.
In another local terminal, connect using the PostgreSQL client:
psql -h 127.0.0.1 -p 15432 -U app_reader -d app_db
Replace app_reader and app_db with your own account and database. Enter the database password when prompted, then test the connection:
SELECT current_database(), current_user, version();
Use q to exit psql. Close the SSH tunnel with Ctrl+C when you finish. If your SSH server uses a custom port, add -p with that port to the SSH command, not the psql command.
3. Connect to PostgreSQL over SSH with DBeaver
A graphical database client is useful when you need to browse schemas, inspect records, or write SQL queries regularly.
DBeaver supports PostgreSQL and SSH tunneling. Create a PostgreSQL connection and configure the main database settings:
| Setting | Example |
|---|---|
| Database host | 127.0.0.1 |
| Database port | 5432 |
| Database | myapp_db |
| Database user | myuser |
Enable SSH tunneling in the connection’s SSH settings. Enter your VPS hostname, SSH port, SSH username, and authentication details. Verify the server host key and test the connection.
DBeaver manages the forwarding when its built-in tunnel is enabled. Alternatively, you can leave that option disabled and connect to 127.0.0.1:15432 using the manual tunnel from the previous section.
Avoid enabling both approaches for the same connection. Refer to the DBeaver SSH configuration documentation for additional settings.
4. Manage PostgreSQL with Server Explorer
Database administration is often part of a larger investigation. You might need to inspect application logs, check a Docker container, review a configuration file, and examine database records during the same session.
Server Explorer is a desktop SSH management application for macOS and Windows with an integrated database workspace supporting PostgreSQL, MySQL, MariaDB, and SQLite.
Connect to your Linux server using your usual SSH credentials, then open the database workspace. Select or configure a PostgreSQL connection according to your deployment and provide the required database credentials.
Confirm that you are working with the intended server and database before inspecting or modifying any records.

Once connected, use the database workspace to browse tables and inspect their contents alongside your other server administration tools.

This approach is especially useful when you manage multiple Linux servers and want to keep database operations in the context of the relevant machine. Available connection options depend on your deployment, application version, and permissions.
Bring your servers into focus.
A desktop app for macOS and Windows. Connect over SSH.
5. Access PostgreSQL running inside Docker
Containerized PostgreSQL requires an additional check. The database may listen on port 5432 inside the container without exposing that port on the VPS itself.
On the server, inspect the running containers:
docker ps
If the database is already accessible on the VPS loopback interface, forward that host port using the SSH command shown earlier.
For a new Docker Compose deployment that requires host-based database access, a loopback-only port mapping can be configured under the database service:
ports:
- "127.0.0.1:5432:5432"
Unlike a generic 5432:5432 mapping, this explicitly binds the published port to the IPv4 loopback interface. Check your Docker version, firewall configuration, and actual network exposure; older Docker versions have known limitations with localhost-published ports.
Do not recreate a production database container just to apply this change without checking persistent storage, backups, and downtime requirements.
Server Explorer also supports database connections for compatible Docker deployments. Inspect the database container and use the database workspace where supported; the exact access method depends on your setup.

6. Troubleshoot PostgreSQL SSH connection errors
A successful SSH login does not necessarily mean PostgreSQL is reachable. Use the error message to narrow down the problem.
| Error | What to check |
|---|---|
| Connection refused | PostgreSQL listener, remote destination port, container mapping |
| Address already in use | Choose a different local forwarding port |
| Password authentication failed | Database username and password |
| No pg_hba.conf entry | Database authentication rules for the connection source |
| Administratively prohibited | SSH server forwarding restrictions |
| Connection timed out | SSH connectivity or reachability of the remote database host |
Run the SSH command again with -v for diagnostic output. For authentication failures, inspect PostgreSQL logs and its pg_hba.conf rules rather than disabling authentication. Remember that PostgreSQL sees the connection coming from the SSH server’s side of the tunnel, not directly from your laptop.
Security best practices
Keep the database port private whenever possible. Use SSH keys, verify server host keys, and assign dedicated database roles with only the privileges required for each task. Prefer read-only access for production investigations.
Do not change listen_addresses to * or add broad trust rules simply to make remote access work. If your database is on another machine beyond the SSH server, consider PostgreSQL TLS as well: SSH encryption covers the tunnel to the SSH server, not necessarily the remaining network path.
Final thoughts
Accessing PostgreSQL over SSH is a practical way to work with remote databases without opening their ports to the public internet. OpenSSH gives you direct control over forwarding, while DBeaver provides a dedicated graphical database workflow.
If database access is one of several tasks you perform on a VPS, Server Explorer brings database inspection together with files, terminals, logs, and Docker management. Choose the workflow that fits your deployment, and keep database permissions and network exposure as restrictive as possible.
Bring your servers into focus.
A desktop app for macOS and Windows. Connect over SSH.