Server-side PostgreSQL client
Supabase-style query DX.
Direct to PostgreSQL.
pg-fluent reuses the postgrest-js builder and { data, error } contract,
then translates your query into a single parameterized SQL statement and runs it over
node-postgres. No PostgREST service. No HTTP hop. No JSON re-serialization.
import { createClient } from 'pg-fluent'
const db = createClient({
connectionString: process.env.DATABASE_URL,
})
const { data, error } = await db
.from('todos')
.select('id, task, done')
.eq('done', false)
.order('id')
.limit(10)
Lazy thenable builders. Await to execute. Results are { data, error, count, status, statusText }.
Why pg-fluent
The builder you know, without the service in the middle.
pg-fluent is for server-side apps that want the PostgREST/Supabase query ergonomics but prefer a direct PostgreSQL connection and normal driver-level control.
Familiar query contract
select, filters, embeds, writes, rpc, and the { data, error } result shape — built on the upstream postgrest-js contract.
One parameterized statement
Queries compile to parameterized SQL and execute through node-postgres. Values are bound; structure is validated and quoted.
Direct Postgres connection
Use a connection string or discrete credentials, your own pool settings, transactions, streaming, COPY, and LISTEN/NOTIFY.
Type generation
Generate a PostgREST-js-compatible Database type from your live schema with pg-fluent gen-types.
Quick start
Install and query in minutes.
pg-fluent is a server-side library. Install it in your Node.js app, point it at a PostgreSQL database, and use the fluent builder you already know.
npm install pg-fluent
Optional peers:
npm install pg-copy-streams # db.copyFrom()
npm install @opentelemetry/api # telemetry: true
import { createClient } from 'pg-fluent'
const db = createClient({
connectionString: process.env.DATABASE_URL,
schemas: ['public'],
})
const { data, error } = await db
.from('users')
.select('id, name')
.eq('active', true)
.order('name')
if (error) throw error
console.log(data)
await db.close()
What you get
Built for real server-side Postgres work.
Beyond basic CRUD, pg-fluent includes the primitives you need when you run directly against a database: transactions, raw SQL, streaming, COPY, LISTEN/NOTIFY, and observability.
Queries & filters
Full filter set, ordering, pagination, JSON paths, single/maybeSingle, count, head, csv, geojson, and explain.
Embedded resources
Many-to-one, one-to-many, many-to-many, aliases, !inner, FK disambiguation, nested embeds, and to-one spreads.
Safe writes
Insert/update/upsert/delete with .select() for returning rows. Unfiltered mutations are blocked by default. .maxAffected(n) protects bulk changes.
Transactions
Dedicated connection per transaction, savepoints, isolation levels, read-only mode, optional retry for serialization/deadlock.
Raw SQL
db.sql binds interpolations as parameters. db.identifier() composes structure safely. db.unsafe() is the explicit escape hatch.
Streaming & COPY
.stream() yields rows via a server-side cursor. db.copyFrom() bulk loads rows via COPY FROM STDIN.
LISTEN / NOTIFY
Dedicated listener connection, auto-reconnect with backoff, channel re-subscribe, and optional onReconnect resync hook.
Replicas & routing
Named connections and read-replica routing for eligible reads. Mutations, RPC, transactions, and schema loads stay on primary.
How it works
Parse. Plan. Parameterize. Execute.
pg-fluent intercepts the PostgREST-style request built by the fluent API, resolves schema metadata from pg_catalog, compiles a SQL plan, and executes it with bound parameters through pg.
- Values are always bound as parameters.
- Identifiers and query structure are validated and quoted.
- Reads can be retried on transient connection failures (configurable).
- Writes are never retried outside an explicit transaction retry.
db.unsafe()is the only API that executes caller-supplied SQL text.
pg-fluent connects directly to PostgreSQL using the connection's own role. It does not emulate Supabase anon/authenticated roles, and it does not set request.jwt.claims.
Row Level Security policies written for Supabase roles do not apply automatically. If you connect as a table owner or a BYPASSRLS role, RLS is bypassed entirely.
Treat database credentials as secrets. Never expose them to browsers or untrusted clients.
pg-fluent is an independent project, not affiliated with or endorsed by Supabase. "Supabase" is used only to describe compatibility with the postgrest-js query style.
Familiar patterns
From simple filters to transactions and raw SQL.
The same client handles fluent queries, safe raw SQL composition, and transactional workflows.
Filters + embedded resources
const { data } = await db
.from('posts')
.select('id, title, author:users(name)')
.eq('published', true)
.order('id', { ascending: false })
Transaction with savepoint
await db.transaction(async (trx) => {
await trx.from('accounts')
.update({ balance: 10 })
.eq('id', 1)
await trx.savepoint(async (sp) => {
// rollback only this step on throw
})
})
Safe raw SQL
const id = 42
const { data, error } = await db.sql`
select id, name
from users
where id = ${id}
`
// ${id} is bound as a parameter
Migration
Moving from supabase-js?
The query builder API is intentionally close. The main changes are the connection model and the security model.
| Area | supabase-js / PostgREST | pg-fluent |
|---|---|---|
| Transport | HTTP to PostgREST | Direct PostgreSQL wire protocol (node-postgres) |
| Credentials | Project URL + anon/service key | Connection string or discrete DB credentials |
| Scope | DB + auth + storage + functions | Database client only |
| RLS / roles | PostgREST role/JWT context | Runs as the connecting DB role (review policies) |
| Extras | Supabase platform APIs | .toSQL(), transactions, raw SQL, streaming, COPY, LISTEN/NOTIFY, replicas |
Get started
Use the Postgres query DX you like, on the connection you control.
Read the docs, generate types from your schema, and keep the runtime model explicit: server-side, direct DB role, parameterized SQL.