Static IP guide

Connecting to Postgres with a static IP from a Go app on Heroku

Go services on Heroku have two good routes into an IP-allowlisted Postgres server. The zero-code option is a Fixie-Wrench tunnel on localhost:5432 (shown below); the pure-Go option is a golang.org/x/net/proxy SOCKS5 dialer plugged into pgx's DialFunc, keeping everything in-process.

RuntimeGo
PlatformHeroku
DestinationPostgres
Fixie productFixie Socks Heroku add-on

Recommended setup

Use Fixie Socks Heroku add-on for Postgres connections to a database that only accepts traffic from approved IP addresses. Store the proxy value in FIXIE_SOCKS_HOST, then configure Go to route outbound traffic through Fixie before connecting to Postgres.

Set up Fixie on Heroku

Heroku is a native Fixie marketplace flow. Install the Fixie Socks Heroku add-on or attach it from the Heroku CLI. Heroku will create FIXIE_SOCKS_HOST as a config var for the app.

Attach fixie-socks to your Heroku app

heroku addons:create fixie-socks
heroku config:get FIXIE_SOCKS_HOST

Implementation

The example below shows the core application-side change. Keep credentials in environment variables and avoid committing proxy, database, or API secrets.

Forward a local port to Postgres through Fixie Socks

# Install Fixie-Wrench in your app build or deploy process.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/usefixie/fixie-wrench/HEAD/install.sh)"

# Start a tunnel from localhost:5432 to postgres.example.com:5432.
# Your Go app connects to localhost:5432; Fixie handles the outbound static IP.
./bin/fixie-wrench 5432:postgres.example.com:5432

Good to know

  • For the in-process route, build the SOCKS5 dialer from the parsed FIXIE_SOCKS_HOST values and assign it as the connection config's DialFunc before connecting.
  • With the tunnel, your DSN is simply host=127.0.0.1 port=5432 — sslmode still negotiates with the real server end-to-end.

Allowlist and test the static IPs

  1. Open the Fixie dashboard and copy the outbound IP addresses for this proxy.
  2. Add both IPs to the Postgres firewall or security group.
  3. Deploy the updated Go app on Heroku.
  4. Run a small connection test before sending production traffic.

Related guides

Related docs