The SQL drivers are two JavaScript packages that send PostgreSQL or MySQL dialect SQL to a SurrealDB instance over HTTP and WebSocket. Use them in runtimes that cannot open a TCP socket, such as Cloudflare Workers, Vercel Edge, Deno Deploy and browsers. They also run on Node.js, Bun and Deno.
| Package | Dialect | RPC method |
|---|---|---|
@surrealdb/pg | PostgreSQL | pgsql |
@surrealdb/mysql | MySQL | mysql |
Each package has a native API (a tagged template plus query, execute, batch and begin) and a compatibility layer, so existing ORMs and query builders run on top of it.
When to use the SQL drivers
Edge, serverless and browser code. These runtimes have
fetchandWebSocketbut no TCP. The drivers use only those APIs.Existing SQL code. Drizzle, Kysely, Prisma, node-postgres, mysql2, Neon and PlanetScale code can point at SurrealDB with a small change.
SurrealQL on the same connection. Every client can also run SurrealQL with the
surqltemplate, with the same namespace, database and authentication.
If your code can open TCP connections, a native driver such as pg or mysql2 can connect to the PostgreSQL or MySQL wire listener of the instance directly. If you want SurrealQL and the full data model from JavaScript, use the JavaScript SDK.
The drivers need a SurrealDB instance that serves the pgsql and mysql RPC methods. A server that does not serve them returns a "Method not found" error. The MySQL value encoding may still change.
A first query
npm install @surrealdb/pgimport { connect } from "@surrealdb/pg";
const sql = connect("postgres://root:secret@abc123.surrealdb.earth/app?namespace=prod");
const name = "Tobie";
const rows = await sql`SELECT name, age FROM person WHERE name = ${name}`;The client sends nothing until the first query. Values in a template are bound as typed parameters and are never interpolated into the SQL text.
How it works
Each statement is one RPC call: pgsql or mysql, with the SQL text, optional variables and optional options. Positional parameters ($1 for PostgreSQL, each ? for MySQL) bind to the variables _1 to _n, and the instance binds them as typed parameters. The reply holds one entry per statement, with its status, command tag, row count, column metadata and rows.
The clients use two transports and pick one by what the call needs.
| HTTP | WebSocket | |
|---|---|---|
| One-shot queries | Yes | Yes |
| Atomic batch | Yes | Yes |
| Interactive transactions | No | Yes |
| Live queries | No | Yes |
| Session settings that last | No, sent with every call | Yes, sent once per connection |
| Cancel a running call | Abort the request | query_cancel on abort or timeout |
With the default transport: "auto", one-shot calls use HTTP, which needs no handshake and has the lowest latency from a cold isolate. Anything that needs a session opens a WebSocket.
Next steps
Installation
Install the packages and check the runtime requirements.
Connections
Connection strings, authentication, transports and settings.
Querying
Tagged templates, query, execute and batch.
Transactions
Atomic batches and interactive transactions.
Results and value types
Result shape and how column types decode.
Errors
Error classes and how to handle them.
SurrealDB extras
SurrealQL, live queries, describe and capabilities.
ORM and driver compatibility
node-postgres, mysql2, Drizzle, Kysely and Prisma.
Neon and PlanetScale
Drop-in entry points for the two serverless clients.