文章背景与核心概要
随着大语言模型(LLM)与前端技术的迅猛发展,开发者在浏览器端运行复杂基础设施的能力正在发生质的飞跃。本文介绍了由 ElectricSQL 团队及相关开源社区推出的 database.build(原名 postgres.new)。这是一个完全运行在浏览器中、由 WebAssembly (WASM) 驱动并通过 AI 深度赋能的 PostgreSQL 沙盒环境。
该工具的核心技术基础是 PGlite——一个直接从真实 Postgres 源码编译而来的 WASM 版本,并结合了 GPT-4o 等大语言模型代理。开发者无需配置复杂的后端基础设施,即可在浏览器中实现拖拽导入 CSV、自动生成图表、构建 ER 图、执行数据库迁移以及运行向量语义搜索。本文详细解析了该工具的工作原理、核心功能(如 AI 自愈机制、pgvector 语义搜索等)、PGlite 的底层技术实现,以及未来在 S3 部署和分享方面的路线图。
database.build (formerly postgres.new): In-browser Postgres with an AI Interface
⚠️ Important Update:
postgres.newis officially renaming todatabase.build. Read why here.
📌 Summary
database.build is an innovative in-browser Postgres sandbox powered by AI assistance. It allows developers to instantly spin up unlimited, disposable PostgreSQL databases that run entirely locally within the browser using WebAssembly (WASM).
Combined with LLM agents (such as GPT-4o) that have full autonomy over the database, users can effortlessly import CSVs, generate charts, build ER diagrams, execute migrations, and perform semantic search without setting up complex backend infrastructure.
database.build 是一个由 AI 辅助驱动的创新型浏览器端 Postgres 沙盒。它允许开发者瞬间启动无限个可丢弃的 PostgreSQL 数据库,这些数据库完全通过 WebAssembly (WASM) 在浏览器本地运行。
结合对数据库拥有完全自主权的 LLM 代理(如 GPT-4o),用户无需配置复杂的后端基础设施,便能轻松导入 CSV、生成图表、构建 ER 图、执行数据库迁移以及进行语义搜索。
Introduction
Introducing database.build, an in-browser Postgres sandbox with AI assistance. With database.build, you can instantly spin up an unlimited number of Postgres databases that run directly in your browser (and soon, deploy them to S3).
Each database is paired with a large language model (LLM), which opens the door to powerful use cases:
- Drag-and-drop CSV import (generating tables on the fly)
- Generating and exporting reports
- Building dynamic charts
- Designing database schemas and diagrams
All of this operates completely locally in your browser. It is essentially Postgres and ChatGPT combined into a seamless interface.
Common Use Cases
- Quick Data Analysis: You have a CSV file and want to query it instantly. Instead of opening Excel, you can use SQL to analyze it directly.
- Verified AI SQL Generation: Instead of guessing if ChatGPT-generated SQL is correct, you can run it instantly against a real, live database sandbox.
- Rapid Prototyping: Describe the app or schema you want, and let AI automatically generate ER diagrams and SQL migrations.
隆重推出 database.build,这是一个配备 AI 辅助的浏览器端 Postgres 沙盒。借助 database.build,你可以瞬间启动数量不限、直接在浏览器中运行的 Postgres 数据库(不久之后还支持部署到 S3)。
每个数据库都与一个大语言模型(LLM)配对,从而开启了许多强大的应用场景:
- 拖拽式 CSV 导入(即时生成数据表)
- 生成并导出报表
- 构建动态图表
- 设计数据库架构与图表
所有这一切都完全在你的浏览器本地运行。它本质上是 Postgres 与 ChatGPT 相结合的无缝交互界面。
常见应用场景
- 快速数据分析: 你手头有一个 CSV 文件并希望立即查询它。与其打开 Excel,不如直接使用 SQL 对其进行分析。
- 已验证的 AI SQL 生成: 不再需要去猜测 ChatGPT 生成的 SQL 是否正确,你可以直接在真实、实时的数据库沙盒中运行它。
- 快速原型开发: 描述你想要的应用程序或架构,让 AI 自动生成 ER 图和 SQL 迁移脚本。
How it Works
All queries in database.build run directly in your browser. There is no remote Postgres container or WebSocket proxy required.
The core technology making this possible is PGlite, a WASM build of Postgres compiled directly from the real Postgres source code, created by the team at ElectricSQL.
工作原理
database.build 中的所有查询都直接在你的浏览器中运行。它不需要任何远程 Postgres 容器或 WebSocket 代理。
实现这一点的核心技术是 PGlite,它是直接从真实的 Postgres 源码编译而来的 Postgres WASM 版本,由 ElectricSQL 团队开发。
Motivation
The development of database.build was driven by three main goals:
- AI-Driven Development: Re-imagining how developers interact with databases via AI, allowing for error-tolerance and iterative self-healing.
- Postgres Sandboxing: Bringing the speed and disposability of notebooks to SQL, allowing developers to spin up millions of lightweight database instances.
- Extremely Cheap Databases: Exploring ways to offer developers scalable, low-cost databases stored efficiently on S3.
创作动机
database.build 的开发主要受到以下三个目标的驱动:
- AI 驱动的开发: 重新构想开发者如何通过 AI 与数据库进行交互,实现容错和迭代式自我修复。
- Postgres 沙盒化: 将 Notebooks(笔记本)的速度和用后即焚特性引入 SQL,允许开发者启动数百万个轻量级数据库实例。
- 极其廉价的数据库: 探索为开发者提供可扩展、低成本且高效存储在 S3 上的数据库的方法。
Features and How They Work
1. AI Assistant
database.build pairs PGlite with a large language model (GPT-4o) and grants it full operational autonomy over the database without requiring constant user confirmation. This allows the AI to perform complex, multi-step operations back-to-back without latency, making the assistant feel far more natural and human-like.
2. CSV Imports and Exports
Simply drag-and-drop a CSV file onto the chat interface to instantly create a table with automatically inferred data types. * AI Self-Healing: If a data type mismatch causes an import error, Postgres feeds the error back to the language model, allowing it to automatically patch and retry the query. * Exports: Ask the AI to export any query result directly into a CSV file for external usage.
3. Charts
By including keywords like "chart" in your prompt, the AI executes the underlying SQL query and renders a visual representation using Chart.js. You can interactively modify the chart's type, color scheme, and axes purely through natural language.
4. ER Diagrams and Migrations
Instead of manually drawing database architectures before writing code, the AI executes real DDL (CREATE and ALTER) statements directly against the sandbox and instantly renders an ER diagram using the schema visualizer.
* All DDL operations are tracked and compiled into clean SQL migration scripts that you can copy for your production apps.
5. Semantic Search and RAG
PGlite features native support for pgvector compiled to WASM. Combined with Transformers.js, database.build allows you to generate text embeddings directly in the browser, store them efficiently, and execute high-speed vector similarity searches locally.
6. Deployments
Coming soon, database.build will introduce read-only deployments hosted cost-effectively on S3. To bridge local WASM instances with standard TCP Postgres clients, the team built pg-gateway—a TypeScript library implementing the server-side Postgres wire protocol.
功能特性及其工作方式
1. AI 助手
database.build 将 PGlite 与大语言模型(GPT-4o)结合,赋予其对数据库的完全操作自主权,而无需用户进行持续确认。这使得 AI 能够毫无延迟地连续执行复杂的多步骤操作,从而让该助手感觉更加自然、像人类一样。
2. CSV 导入与导出
只需将 CSV 文件拖放到聊天界面中,即可根据自动推断的数据类型即时创建表。 * AI 自我修复: 如果数据类型不匹配导致导入错误,Postgres 会将错误反馈给大语言模型,允许其自动修补并重试查询。 * 导出: 让 AI 将任何查询结果直接导出为 CSV 文件供外部使用。
3. 图表
通过在提示词中包含“chart(图表)”等关键词,AI 将执行底层 SQL 查询并使用 Chart.js 渲染可视化图形。你可以完全通过自然语言交互式地修改图表的类型、配色方案和坐标轴。
4. ER 图与迁移
与其在编写代码前手动绘制数据库架构,不如让 AI 直接针对沙盒执行真实的 DDL(
CREATE和ALTER)语句,并使用 schema visualizer(架构可视化工具) 即时渲染 ER 图。 * 所有 DDL 操作都会被跟踪并编译成整洁的 SQL 迁移脚本,你可以将其复制到生产应用程序中使用。5. 语义搜索与 RAG
PGlite 对编译为 WASM 的 pgvector 提供了原生支持。结合 Transformers.js,database.build 允许你直接在浏览器中生成文本嵌入(embeddings),高效存储它们,并在本地执行高速的向量相似度搜索。
6. 部署
database.build 即将推出以极高性价比托管在 S3 上的只读部署。为了将本地 WASM 实例与标准 TCP Postgres 客户端连接起来,团队构建了 pg-gateway——这是一个实现了服务端 Postgres 线端协议(wire protocol)的 TypeScript 库。
PGlite Deep Dive
None of this would be possible without PGlite.
What is PGlite?
PGlite packages Postgres into a lightweight WASM bundle integrated with a TypeScript/JavaScript client library. It runs seamlessly in browsers, Node.js, and Bun with zero external dependencies, making it ideal for: * Unit and CI testing (fresh DB instances per test) * Local development environments * In-browser web containers
Data Persistence & Extensions
- Storage: Uses IndexedDB and OPFS (Origin Private File System) in the browser, achieving CRUD execution speeds under 0.3ms.
- Extensions: Supports powerful extensions like
pgvectorfor AI workflows andlivefor real-time reactive queries.
Technical Overview: Running Postgres in WASM
Because WASM lacks process-forking and multi-threading capabilities, PGlite utilizes Postgres's built-in "single user mode" and builds custom wrappers around it to support:
* User connections and Row Level Security (RLS)
* Virtual file system integration (/dev/blob) for native COPY commands
* dumpDataDir and loadDataDir for tarball persistence
PGlite 深度解析
如果没有 PGlite,这一切都不可能实现。
什么是 PGlite?
PGlite 将 Postgres 打包成一个轻量级的 WASM 包,并集成了一个 TypeScript/JavaScript 客户端库。它可以在浏览器、Node.js 和 Bun 中无缝运行,且零外部依赖,使其成为以下场景的理想选择: * 单元测试和 CI 测试(每个测试拥有全新的数据库实例) * 本地开发环境 * 浏览器端 Web 容器
数据持久化与扩展
- 存储: 在浏览器中使用 IndexedDB 和 OPFS(源私有文件系统,Origin Private File System),实现的 CRUD 执行速度低于 0.3 毫秒。
- 扩展: 支持诸如用于 AI 工作流的
pgvector以及用于实时响应式查询的live等强大扩展。技术概述:在 WASM 中运行 Postgres
由于 WASM 缺乏进程派生(process-forking)和多线程功能,PGlite 利用了 Postgres 内置的“单用户模式”(single user mode)并在其周围构建了自定义包装器,以支持: * 用户连接和行级安全性(RLS) * 用于原生
COPY命令的虚拟文件系统集成(/dev/blob) * 用于 tarball 持久化的dumpDataDir和loadDataDir
Coming Soon
The roadmap includes several upcoming features:
* Database Deployments: Host and access your database globally via S3.
* Expanded File Support: Parse Word documents, images, and raw binary assets.
* Database Sharing: Share live playgrounds instantly via unique URLs.
* OPFS Support: Upgrade local storage to use the high-performance Origin Private File System.
* Database Exporting: Full pg_dump support to export data back to standard PostgreSQL servers.
即将推出
产品路线图包含以下即将推出的功能:
- 数据库部署: 通过 S3 在全球范围内托管和访问你的数据库。
- 扩展的文件支持: 解析 Word 文档、图像和原始二进制资产。
- 数据库分享: 通过唯一的 URL 瞬间分享实时的 playground 环境。
- OPFS 支持: 将本地存储升级为使用高性能的源私有文件系统(OPFS)。
- 数据库导出: 完全支持
pg_dump,以便将数据导出回标准的 PostgreSQL 服务器。
Open Source Repositories
The entire ecosystem is open source and permissively licensed. Check out the core repositories below:
- PGlite (Apache 2.0): The WASM build of Postgres.
- pg-gateway (MIT): Server-side Postgres wire protocol implementation.
- database-build (Apache 2.0): The frontend interface for database.build.
- transformers.js: Run machine learning models directly in the browser.
开源仓库
整个生态系统都是开源的,并采用了宽松的开源许可。请查看以下核心仓库:
- PGlite (Apache 2.0):Postgres 的 WASM 构建版本。
- pg-gateway (MIT):服务端 Postgres 线端协议实现。
- database-build (Apache 2.0):database.build 的前端界面。
- transformers.js:直接在浏览器中运行机器学习模型。