Run a command before connecting

Last updated: December 3, 2026. Tested with SQLPro Studio 2026.331 on macOS.

If you can only reach your database after running something like kubectl port-forward or aws ssm start-session, SQLPro Studio can run it for you. Open the connection, expand Before Connecting, and put the command (or the path to a script) in Run command. Point the connection at the local end of the tunnel, usually 127.0.0.1 and the local port the command opens. When you connect, SQLPro Studio starts the command, waits for that port to open, connects through it, and stops the command when you disconnect or quit.

No more opening Terminal first and leaving a window running. Each connection keeps its own command, and the command is stored in the keychain on that Mac only. Before Connecting is in the Mac app's PostgreSQL, MySQL, SQL Server, Oracle and MongoDB connection editors (SQLPro Studio 2026.331 and later).

Watch it

Run a Command Before Connecting (kubectl port-forward, AWS SSM) on a Mac (video, 2:00) Video tutorial ยท 2:00 on YouTube

Recorded in SQLPro Studio 2026.331 with a sample database. More tutorials: youtube.com/@hankinsoftdev

When to use it

Use it when the database is not reachable directly and a command opens a local port that forwards to it. Common cases:

If you only need a plain SSH tunnel, you don't need a command: use the connection's built-in SSH Tunnel section instead (see SSH tunnels explained).

Step 1: Point the connection at the tunnel

Click Connect…, then the + menu, and choose your database type, or edit a connection you already have. Fill in the form as if the database were on your Mac:

Step 2: Add the command

Expand Before Connecting and type the command, or the full path to a script, in Run command.

A PostgreSQL connection in SQLPro Studio with Before Connecting expanded and a tunnel script in Run command
A PostgreSQL connection in SQLPro Studio with Before Connecting expanded and a tunnel script in Run command

Some examples, with the local port matching the connection's port:

# Kubernetes
kubectl port-forward -n production svc/postgres 15432:5432

# AWS Systems Manager port forwarding to an RDS endpoint
aws ssm start-session --profile prod --region us-east-1 --target i-0123456789abcdef0 --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters '{"host":["mydb.abc123.us-east-1.rds.amazonaws.com"],"portNumber":["5432"],"localPortNumber":["15432"]}'

# Cloudflare Access
cloudflared access tcp --hostname db.example.com --url localhost:15432

# Your own script
~/scripts/db-tunnel.sh

A script is the better choice when you need more than one step. This one signs in to AWS SSO only when the session has expired, which opens your browser, and then starts the port forward:

#!/bin/zsh -l
PROFILE=my-sso-profile

aws sts get-caller-identity --profile $PROFILE >/dev/null 2>&1 || aws sso login --profile $PROFILE

aws ssm start-session \
  --profile $PROFILE \
  --region us-east-1 \
  --target i-0123456789abcdef0 \
  --document-name AWS-StartPortForwardingSessionToRemoteHost \
  --parameters '{"host":["mydb.abc123.us-east-1.rds.amazonaws.com"],"portNumber":["5432"],"localPortNumber":["15432"]}'

Make the script executable with chmod +x. The command keeps running in the background while you are connected, so it should not exit after opening the tunnel.

Step 3: Install the helper (once per Mac)

macOS does not let SQLPro Studio start other programs by itself, so the first time you use Run command it needs a small helper script in its Scripts folder. Click Install Helper…, then Install…, and in the save panel that follows click Install without changing the name or the folder.

The one-time prompt: Allow SQLPro to run commands?
The one-time prompt: Allow SQLPro to run commands?
The save panel: keep the name sqlpro-command-runner and the suggested folder, then click Install
The save panel: keep the name sqlpro-command-runner and the suggested folder, then click Install

You only do this once on each Mac; every connection uses the same helper. To remove it, delete sqlpro-command-runner from SQLPro Studio's folder in ~/Library/Application Scripts.

Step 4: Connect

Click Save & Connect. SQLPro Studio starts the command, waits until the local port accepts connections, and then connects through it. Browse and query as you normally would; every query goes through the tunnel.

Connected through the tunnel on 127.0.0.1: the orders table opened with a double-click
Connected through the tunnel on 127.0.0.1: the orders table opened with a double-click

When you are done, right-click the connection and choose Disconnect, or quit SQLPro Studio. Either one stops the command, so the tunnel closes with the connection.

How it behaves

Troubleshooting

"the SQLPro command helper is not installed on this Mac"

Edit the connection, expand Before Connecting and click Install Helper…. If you already did, check that you kept the name and folder in the save panel.

"command not found" in the error

The tool is not in the PATH listed above. Use its full path (run which aws in Terminal to find it), or set PATH at the top of a script.

"did not open 127.0.0.1:15432 within 10 minutes"

The command is running but listening on a different port, or it is waiting for input it cannot get. Check that the local port in the command matches the connection's Port field, and run the command in Terminal once to see what it prints.

The connection fails right after the tunnel opens

The tunnel works but the far end does not: the remote host or port in the command is wrong, or the bastion or pod cannot reach the database. The database's own error (for example "server closed the connection unexpectedly") is shown.

Questions people ask

Can SQLPro Studio run kubectl port-forward before it connects?

Yes. Put the kubectl port-forward command in the connection's Before Connecting > Run command field and set the host to 127.0.0.1 and the port to the local port it opens. SQLPro Studio starts it when you connect, waits for the port and stops it when you disconnect.

Does it work with AWS SSM Session Manager and AWS SSO?

Yes. Use aws ssm start-session with the AWS-StartPortForwardingSessionToRemoteHost document, or a script that runs aws sso login first. SQLPro Studio waits up to 10 minutes for the local port, so a browser sign-in has time to finish.

Why does SQLPro Studio need a helper to run commands?

macOS keeps the app in a sandbox that cannot start other programs on its own. The helper is a small script you approve once in a save panel; it runs your command and stops it again. You can remove it by deleting the file.

When does SQLPro Studio stop the command?

When you disconnect the connection or quit SQLPro Studio.

Which connections support Before Connecting?

PostgreSQL, MySQL, SQL Server, Oracle and MongoDB connections in SQLPro Studio for Mac, version 2026.331 and later.

Is SQLPro Studio free?

There is a free 7-day trial on macOS. After that it is $15.99 a month or $99.99 a year (covering macOS, iPhone, iPad, Android and Windows), or $219.99 for a lifetime license on one platform. Students can get it free for a year.

More setup guides: databases and cloud services SQLPro Studio works with, and SSH tunnels for remote database access.