Skip to main content

How to Expose PostgreSQL Remotely Using ngrok

ยท 2 min read

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 PG

For 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 Postgres Locally#

Before we begin, you'll need to have Postgres 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 Postgres 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 Postgres#

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 Postgres#

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

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

Run Postgres

That'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 Postgres to Meroxa:

meroxa resource add 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 Postgres 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 Postgres table. For more, see Postgres Connector Documentation.

Helpful Resources:

I can't wait to see what you build ๐Ÿš€.