How I Got Postgres into a Single Binary
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
How I Got Postgres into a Single Binary 2026-11-20 · 6 min · hyper9databaseswebframe
我是如何将 Postgres 打包成单个二进制文件的 2026-11-20 · 6 分钟 · hyper9databaseswebframe
SQLite, but Postgres SQLite is one of my favorite database technologies. It’s a full SQL database, designed as an embeddable library, that stores all its data in a single file. The sqlite3 command line tool is great too. You point it at a file and run queries. There’s no server to run.
SQLite,但它是 Postgres。SQLite 是我最喜欢的数据库技术之一。它是一个完整的 SQL 数据库,设计为可嵌入的库,将所有数据存储在单个文件中。sqlite3 命令行工具也非常棒。你只需指向一个文件即可运行查询,无需运行任何服务器。
At the other end of the spectrum is Postgres. It’s the most capable general purpose SQL database, trusted in production at scale, and is something of an industry standard. With that comes complexity, starting with the fact that it’s client-server. SQLite is a natural fit for software that needs a database but isn’t meant to be deployed in the cloud at scale: desktop apps, self-hosted tools, things you run locally. These applications can use SQLite and manage it themselves, so you barely know there’s a database involved.
处于另一个极端的是 Postgres。它是功能最强大的通用 SQL 数据库,在大规模生产环境中备受信任,并且已成为某种行业标准。随之而来的是复杂性,首先它是一个客户端-服务器架构。SQLite 非常适合那些需要数据库但不需要大规模云端部署的软件:桌面应用、自托管工具以及你在本地运行的程序。这些应用程序可以使用 SQLite 并自行管理,因此你几乎感觉不到数据库的存在。
So when you find self-hostable open source software you’d like to run on your own computer, it can be daunting when it says it requires Postgres. Now you’re responsible for setting up a database and getting it ready for the software to use. Sure, you can run Postgres in Docker, and plenty of projects recommend it. But now you have two dependencies. If you don’t already have Docker, you have to install it, which can mean a VM and a daemon on your machine. Then you have to run Postgres in it and make sure it’s running whenever you use the software. That’s a lot just to try something you don’t know you want yet.
因此,当你发现想要在自己的电脑上运行的自托管开源软件时,如果它要求必须有 Postgres,这可能会让人望而却步。现在你需要负责设置数据库并使其准备就绪以供软件使用。当然,你可以在 Docker 中运行 Postgres,许多项目也推荐这样做。但现在你有了两个依赖项。如果你还没有安装 Docker,就必须先安装它,这可能意味着要在你的机器上运行一个虚拟机和一个守护进程。然后你还得在其中运行 Postgres,并确保每次使用该软件时它都在运行。仅仅为了尝试一个你还不确定是否需要的东西,这实在太麻烦了。
Wouldn’t it be great to have the SQLite experience, but with Postgres? That’s miniPG . It isn’t Postgres as a library. There’s already a WASM project for that, reasonably called PGlite. miniPG is closer to the SQLite CLI: a single program that embeds a full Postgres server and client, wrapped up so you can use it the way you’d use sqlite3. Point it at a path, that path becomes the database, and you can run queries against it right away. There’s no server to set up or keep running.
如果能拥有 SQLite 的体验,同时又能使用 Postgres,那该多好?这就是 miniPG。它不是作为库存在的 Postgres。已经有一个名为 PGlite 的 WASM 项目实现了这一点。miniPG 更接近 SQLite CLI:它是一个嵌入了完整 Postgres 服务器和客户端的单一程序,经过封装,你可以像使用 sqlite3 那样使用它。指向一个路径,该路径就变成了数据库,你可以立即对其运行查询。无需设置或维护任何服务器。
Jumping into a new Postgres database to try something out or debug it is now easier than using Postgres in a Docker container, and you don’t need Docker at all. And miniPG still provides a fully functional Postgres server for software that needs one. In many cases, you can just launch an open source program that looks for its Postgres config in the environment with miniPG.
现在,进入一个新的 Postgres 数据库进行尝试或调试,比在 Docker 容器中使用 Postgres 更容易,而且你根本不需要 Docker。同时,miniPG 仍然为需要它的软件提供了一个功能齐全的 Postgres 服务器。在许多情况下,你只需启动一个开源程序,并让它通过 miniPG 在环境中查找其 Postgres 配置即可。
miniPG in 30 seconds miniPG comes as a single, self-contained executable. You can just point it at a directory and run a query. There’s no server to start, nothing to configure, and the result comes back immediately.
30 秒了解 miniPG。miniPG 是一个单一的、自包含的可执行文件。你只需将其指向一个目录并运行查询即可。无需启动服务器,无需配置任何内容,结果会立即返回。
minipg ./mydb “create table notes (id serial, body text)” minipg ./mydb “insert into notes (body) values (‘hello’)” minipg ./mydb “select * from notes”
(代码示例保持不变)
That directory is the database. When nothing is using it, it’s just files on disk. You can copy it, back it up, or delete it. This is actually real Postgres, not a compatibility layer, and this binary compiles to Mac, Windows, and Linux.
该目录就是数据库。当没有程序使用它时,它只是磁盘上的文件。你可以复制、备份或删除它。这实际上是真正的 Postgres,而不是兼容层,并且该二进制文件可以编译到 Mac、Windows 和 Linux 上。
When some software expects a Postgres server, miniPG can be one: minipg serve ./mydb -p 5432
当某些软件需要 Postgres 服务器时,miniPG 可以充当该角色: minipg serve ./mydb -p 5432
Like I mentioned before, it can even launch programs with environment variables that configure it for this Postgres database: minipg run ./mydb — some-tool
正如我之前提到的,它甚至可以通过环境变量启动程序,从而为该 Postgres 数据库进行配置: minipg run ./mydb — some-tool
When the program is terminated, there’s no database daemon to leave around or remember to stop. Only the data itself is kept right there on disk, ready to be used next time. Instructions for getting it and even an interactive demo are available on the miniPG website.
当程序终止时,不会有遗留的数据库守护进程需要处理或记得停止。只有数据本身保留在磁盘上,随时准备在下次使用。获取它的说明甚至交互式演示都可以在 miniPG 网站上找到。
Who is it for? Software people, from developers to open source power users, and agents too, can use miniPG whenever they’d reach for SQLite but need Postgres. Running software that only supports Postgres. This is what got me started. Self-hosting a tool shouldn’t always mean operating a database server first. Embedding Postgres in your own programs. If your tool needs Postgres, and PGlite doesn’t cut it, you can ship and use miniPG. Users who don’t care won’t have to think about it, and users who want control can use it like sqlite3 to work with their data.
它适合谁?软件从业者,从开发人员到开源高级用户,甚至是智能体(agents),只要他们想用 SQLite 但又需要 Postgres 时,都可以使用 miniPG。运行仅支持 Postgres 的软件——这就是我开始做这个的原因。自托管工具不应该总是意味着必须先运行一个数据库服务器。在自己的程序中嵌入 Postgres——如果你的工具需要 Postgres,而 PGlite 无法满足需求,你可以分发并使用 miniPG。不在乎的用户无需考虑它,而想要控制权的用户可以像使用 sqlite3 一样使用它来处理数据。
SQLite-style apps that need Postgres features. JSONB, full text search, window functions, extensions like pgvector or PostGIS. Postgres does so much more than SQLite, but it often wasn’t worth the dependency. Learning and experimenting with Postgres. No server, no config. Point it at a directory and start typing SQL. Tests, local dev, and agents. A throwaway database per test, or a local copy of the same database you run in production, without Docker. Agents can spin up a real Postgres for a task and throw it away when they’re done. This really makes Postgres more viable for scenarios it wasn’t before. I’d love to hear the use cases I haven’t thought of.
需要 Postgres 特性的 SQLite 风格应用。例如 JSONB、全文搜索、窗口函数,以及像 pgvector 或 PostGIS 这样的扩展。Postgres 的功能远超 SQLite,但通常为了它而引入依赖并不划算。学习和试验 Postgres——无需服务器,无需配置。指向一个目录即可开始编写 SQL。测试、本地开发和智能体——每个测试使用一个一次性数据库,或者在没有 Docker 的情况下运行生产环境数据库的本地副本。智能体可以为任务启动一个真正的 Postgres,并在完成后将其丢弃。这确实使 Postgres 在以前无法适用的场景中变得更加可行。我很乐意听到我还没想到的用例。
Embedding Real Postgres miniPG is the result of several attempts at embedding real Postgres into a standalone, cross platform binary. Attempt 1: PGlite The obvious place to start was PGlite. The name says it all, and in a sense it delivers: it’s Postgres compiled to WebAssembly. In Go, you can load the module with the wazero runtime, run Postgres inside your program, and have your app connect to it. That’s very close to the SQLite library experience. Brad Fitzpatrick even made a proof of concept that does this in a way that makes PGlite work as a driver for the standard Go SQL package. The catch is that PGlite only supports a single connection and a single session. Software written for a real Postgres server often assumes it can have more. A common pattern is to start a transaction, then make another query on a separate connection before committing. With only one connection, that second query waits for a transaction that’s waiting on it. The app I was trying to run deadlocked.
嵌入真正的 Postgres。miniPG 是多次尝试将真正的 Postgres 嵌入到独立的跨平台二进制文件中的结果。尝试 1:PGlite。最显而易见的起点是 PGlite。名字说明了一切,在某种意义上它确实做到了:它是编译为 WebAssembly 的 Postgres。在 Go 中,你可以使用 wazero 运行时加载该模块,在程序内部运行 Postgres,并让你的应用连接到它。这非常接近 SQLite 库的体验。Brad Fitzpatrick 甚至做了一个概念验证,使 PGlite 可以作为标准 Go SQL 包的驱动程序。问题在于 PGlite 只支持单个连接和单个会话。为真正的 Postgres 服务器编写的软件通常假设它可以有更多连接。一种常见的模式是启动一个事务,然后在提交之前在另一个连接上进行查询。由于只有一个连接,第二个查询会等待正在等待它的事务。我试图运行的应用程序因此死锁了。
Attempt 2: a VM in a webview If I needed real Postgres, I could run real Postgres in a virtual machine I could embed. I have a soft spot for using webviews as portable containers, so I ran v86, an x86 emulator for the browser, inside one, and booted a full Postgres server in it.
尝试 2:webview 中的虚拟机。如果我需要真正的 Postgres,我可以在一个我可以嵌入的虚拟机中运行它。我偏爱使用 webview 作为便携式容器,所以我运行了 v86(一个用于浏览器的 x86 模拟器),并在其中启动了一个完整的 Postgres 服务器。