Open source postgres-wasm brings in-browser PostgreSQL, usefulness unclear

PostgreSQL has been ported to WebAssembly in an open source project called postgres-wasm, created by database developer company Snaplet and cloud database provider Supabase. PostgreSQL in wasm was first demonstrated in August by Postgres solution provider CrunchyData, as a means of offering guided tutorials for the database manager, but this project (unlike PostgreSQL itself) was not open source.

Now Snaplet founder Peter Pistorius says that “We were inspired by Crunchydata to replicate the feat, but also, to open-source the work,” and collaborated with Supabase on the implementation.

PostgreSQL running in the browser via Wasm

It is not only PostgreSQL running in the browser but also Linux, using a small distro called Buildroot and the project v86 which emulates an x86 PC in Wasm, a remarkable achievement in itself.

“We were able to create an 12MB snapshot image capable of running a PostgreSQL server,” said Pistorius.

The I/O performance is poor though, Pistorius explained, “since one of the goals of a VM (and a browser) is to create an isolated environment and we’re attempting to break out of that.” A related issue is getting data in and out of the VM, with the current solution being “a virtual networking device in the VM that communicates via WebSocket to a proxy.”

Supabase engineer Mark Burggraf has posted in detail about the project, noting that the company’s demo site will download around 30MB in order to work. Developers can also run it locally by cloning the github repository.

The demo is impressive in terms of features, implementing PostgreSQL 14.5 and the psql command-line interface. There is some incongruity in the fact that persisting the in-browser database is a matter of saving the entire VM to local storage using IndexedDB, a native browser database that is a more practical solution for most purposes – though not, of course, for implementing a PosgreSQL tutorial.

This is not a handy local database easily queried with JavaScript. What is the use of it? “At this stage, running Postgres in the browser isn’t great for general use-cases,” admits Burggraf. Like Pistorius, he lists “tutorials and demos” as one potential use, also mentioning potential for offline data analysis, testing, in-browser development environments, or sharing support issues.

It would be more useful if one could compile PostgreSQL direct to Wasm with no VM requirement, but it is not easy because of dependencies between the database manager and operating system features.

Despite the lack of obvious use cases, the project is amazing and shows that there are few limitations on what can be persuaded to run in a browser.  “Making Postgres Wasm helped v86[0] to find a new bug,” said one commenter, adding that it showcases “the possibilities of Wasm and how you can overcome the current challenges.”