How to Expose PostgreSQL Remotely Using ngrok

By   Taron Foxworth

 10 May 2021

In this guide, we will walk through exposing a local PostgreSQL instance with ngrok. This method allows you to quickly test and analyze the behavior of PostgreSQL with data platforms like Meroxa.Add Local PGFor this example, we are going to use ngrok. ngrok exposes local servers behind NATs and firewalls to the public internet over secure tunnels.

Let's begin.

Step One: Running PostgreSQL Locally

Before we begin, you'll need to have PostgreSQL installed and running locally. The easiest and quickest way using Docker:

$ docker run --rm -p 5432:5432 -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=demo postgres
Run Postgres

For more details on configuration, see postgres on Docker Hub.


Now that PostgreSQL is running on port 5432, you can connect to the local database outside of the container using psql :

$ psql -U postgres -h localhost -p 5432 postgres


Step Two: Running ngrok and Exposing PostgreSQL

Next, we can create a tunnel using ngrok and expose the locally running database.

First, you'll need to download and install ngrok, and create an account. Then, you can start the tunnel by running the following:

$ ngrok tcp 5432

Run Ngrok

For more information, see ngrok tcp.

Note: You'll need to create an ngrok account to use tcp forwarding.

Step Three: Connecting to PostgreSQL

Now that PostgreSQL and ngrok are running, you can connect to the publically exposed database using psql:

$ psql -h -p 17618 -U postgres -d postgres>

Run PostgresThat's it! You can now connect to your local instance over the internet.

What's next?

This method super helpful to quickly test and analyze behavior using PostgreSQL with cloud services. For example, you can add the local PostgreSQL to Meroxa:

$ meroxa resource create localpg --type postgres --url "postgres://"

Note: Since our database is local, SSL is not enabled by default. To connect, you'll need to append ?sslmode=disable to the PostgreSQL connection URL.

By adding it as a Meroxa Resource, you can easily capture real-time CDC events for every insert, update, delete operation from a local PostgreSQL table. For more, see PostgreSQL Resource Documentation.

Helpful Resources:

I can't wait to see what you build 🚀.

     PostgreSQL, ngrok