DEV Community

Cover image for Colab + Postgres + ngrok: A whopping trio!

Posted on

Colab + Postgres + ngrok: A whopping trio!

In the previous post, we saw how to run Postgres on Google Colab and interface with it through psql as well as from Python itself. This only allowed accessing the database from within the Colab notebook - there was no way to connect from other applications or devices on the internet.

In this post, we will explore how to publicly expose the Postgres instance using ngrok, allowing secure connections from anywhere.

Wait. What is Ngrok?

Ngrock is a tunneling tool that allows you to expose a web service running on your local machine to the public internet over HTTPS. Here are some key things to know about ngrok:

  • It creates a secure tunnel between a public URL and a port on your local machine. This lets you share your locally-running services (like web apps) with others.

  • When you run ngrok, it provides you with a public URL that maps to your local server. For example, it might create a URL like that tunnels to localhost:8080.

  • The URLs it generates are random subdomains under the ngrok domain, and change each time you restart ngrok. This helps protect your local server from being directly exposed.

  • It supports TCP and HTTP(S) tunnels. So you can expose web servers, databases, APIs and more using any protocol.

  • The tunnel is secure - it uses HTTPS and has built-in authentication. Ngrok handles encrypting and decrypting traffic between endpoints automatically.

So we can use ngrok to accessing our database from outside the local network, and exposing APIs for testing mobile or web clients or whoever we want.

Setting up ngrok

To start, we'll install ngrok on Colab:

ngrok install and run

You can grab your authtoken from the ngrok portal. To verify, you can open your Agents tab which is under Tunnels and you can the session that was started.

ngrok session

Connecting from another machine

Now on another (our local) computer, we can use the ngrok URL instead of localhost to reach our database.

We pass the ngrok URL along with username, password and database. The SSL connection is required due to ngrok's encryption.

local notebook

You should now be connected to the Colab Postgres instance from your local machine! We can run queries and interact with the database remotely. Now your Python application has full access to the database running on Colab. The ngrok tunnel handles secure communication behind the scenes.

Other use cases

Exposing the database publicly opens up many possibilities beyond local experimentation:

  • Build test applications and connect from your laptop
  • Demo prototypes to clients without sharing Colab link
  • Integrate Colab work with larger apps via the database
  • Perform multi-user testing of database-backed services
  • Connect a web app to Postgres on Colab for development

Ngrok handles authentication securely, so you don't have to publicly expose credentials or ports. It's a very convenient tool for bringing services outside Colab.

Additionally, it might be possible to save your database on you google drive and then import your drive to continue between sessions.


In this post we saw how to use ngrok to forward port 5432 and make the Postgres instance on Google Colab publicly accessible via a web URL. This lets us connect and query the database from other machines, scripts, and applications securely.

Combined with Postgres and Ngrok, Colab provides a powerful free environment for developing, testing and prototyping full-stack database applications right from the browser. Ngrok opens new possibilities beyond the single notebook model, breaking Colab out of isolation when needed.


Top comments (0)