superapp
BackendDatabases

PostgreSQL

Connect to PostgreSQL databases.

Chat in Claude

Connect to a PostgreSQL database and get full CRUD through a direct pg driver connection.

import { createEngine } from '@superapp/backend'

const engine = createEngine({
  connections: {
    main: process.env.PG_URL!,
  },
})

Connection URL Format

Standard PostgreSQL connection string:

postgres://user:password@host:5432/database?sslmode=require
ParameterDescription
userDatabase user
passwordDatabase password
hostServer hostname or IP
5432Port (default: 5432)
databaseDatabase name
sslmodedisable, require, verify-ca, verify-full

How It Works

When the engine starts, it detects the postgres:// protocol and initializes a connection pool using the pg driver. All tables in the target database become available under the main namespace. Queries are executed directly against Postgres with no intermediary.

Capabilities

OperationSupported
findMany / findOneYes
createYes
updateYes
deleteYes
count / aggregateYes
TransactionsYes

Full Example

import { createEngine } from '@superapp/backend'
import { betterAuthProvider } from '@superapp/backend/auth/better-auth'
import { createHonoMiddleware } from '@superapp/backend/adapters/hono'
import { Hono } from 'hono'
import { serve } from '@hono/node-server'

const engine = createEngine({
  connections: {
    main: process.env.PG_URL!,
  },
  auth: betterAuthProvider({
    secret: process.env.AUTH_SECRET!,
    userTable: {
      table: 'main.users',
      matchOn: { column: 'id', jwtField: 'id' },
      columns: ['id', 'email', 'name'],
    },
  }),
  permissions: {
    orders: {
      table: 'main.orders',
      select: {
        roles: ['viewer'],
        columns: ['id', 'amount', 'status', 'customer_id', 'created_at'],
      },
    },
  },
})

const app = new Hono()
app.route('/', createHonoMiddleware(engine))
serve({ fetch: app.fetch, port: 3001 })

Multiple Postgres Connections

Attach several Postgres databases under different names:

const engine = createEngine({
  connections: {
    main: process.env.PG_URL!,
    analytics: process.env.ANALYTICS_PG_URL!,
  },
})

Tables are namespaced: main.orders, analytics.events.

Troubleshooting

ProblemCauseFix
Connection refusedPostgres not reachableCheck host, port, and firewall rules
password authentication failedWrong credentialsVerify user/password in connection URL
SSL connection requiredServer requires SSLAdd ?sslmode=require to the URL
relation does not existTable not in target databaseCheck database name in the URL
too many connectionsPool exhausted on Postgres sideIncrease max_connections or use a connection pooler like PgBouncer

On this page