For this example, we are going to use ngrok. ngrok exposes local servers behind NATs and firewalls to the public internet over secure tunnels.
Step One: Running PostgreSQL Locally
docker run --rm -p 5432:5432 -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=demo 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 -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.
ngrok tcp 5432
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 -h 0.tcp.ngrok.io -p 17618 -U postgres -d postgres
That's it! You can now connect to your local instance over the internet.
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://postgres:[email protected]:19272/demo?sslmode=disable"
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.
I can't wait to see what you build 🚀.