What it does
PostgreSQL adapter stores permissions and roles in your Postgres database using the pg driver. It works with standard Postgres, Supabase, Neon, AWS RDS, and other Postgres-compatible databases.
When to use it
Use PostgreSQL when:
- Your app already uses Postgres
- You want permissions in your main database
- You're using Supabase, Neon, or RDS (they all use Postgres)
- You need ACID transactions and reliability
How it works
- Install the driver: permly uses
pgfor async queries - Create a pool: use
pg.Pooland point it at your database - Create adapter: pass the pool to
postgresAdapter() - Create tables: run
npx permly migrateto create the fiveperm_*tables - Use permly: all queries work through the adapter
By default, tables are created in the public schema. You can put them in a different schema (like auth) by passing schema: "auth" to the adapter.
Transactions ensure that concurrent changes to the same user's roles are safe. Postgres handles locking automatically.
Examples
// @typecheck-only: needs pg driver and real PostgreSQL database
import pg from "pg";
import { createPermissions } from "permly";
import { postgresAdapter } from "permly/postgres";
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
const perms = createPermissions({
adapter: postgresAdapter(pool),
permissions: ["posts.create", "posts.edit", "posts.delete"],
roles: ["editor", "viewer"],
});
await perms.sync();
await perms.user(1).assignRole("editor");
const allowed = await perms.user(1).can("posts.edit");
console.log(allowed); // true
// @typecheck-only: needs pg driver and real PostgreSQL database
import pg from "pg";
import { createPermissions } from "permly";
import { postgresAdapter } from "permly/postgres";
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
const perms = createPermissions({
adapter: postgresAdapter(pool),
permissions: ["posts.create", "posts.edit", "posts.delete"],
roles: ["editor", "viewer"],
});
await perms.sync();
const allowed: boolean = await perms.user(1).can("posts.edit");
Connection options
You can customize the schema and table prefix. Pass an options object as the second argument:
// @typecheck-only: needs pg driver and real PostgreSQL connection pool
import pg from "pg";
import { postgresAdapter } from "permly/postgres";
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
const adapter = postgresAdapter(pool, {
schema: "auth", // put tables in auth schema (must exist)
prefix: "perm_" // default prefix
});
SSL and cloud providers
Supabase, Neon, and AWS RDS require SSL. Configure it in your connection string:
// @typecheck-only: needs pg driver and real PostgreSQL connection pool
import pg from "pg";
const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL + "?sslmode=require"
});
Or use the pool options:
// @typecheck-only: needs pg driver and real PostgreSQL connection pool
import pg from "pg";
const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false }
});
For self-signed certificates, rejectUnauthorized: false skips verification.
PgBouncer (connection pooler)
Supabase and Neon provide a pooled connection URL for PgBouncer. Use it for serverless functions or many concurrent connections. permly works fine with PgBouncer's transaction mode because it:
- Uses unnamed statements (not named cursors)
- Creates its own transaction scope (doesn't rely on connection state)
- Doesn't assume persistent connections
Run migrations against the direct URL (not the pooled URL) if your provider recommends it.
TypeScript and esModuleInterop
If TypeScript complains about import pg being the wrong type, enable esModuleInterop in tsconfig.json:
{
"compilerOptions": {
"esModuleInterop": true,
"moduleResolution": "NodeNext"
}
}
This is the default in new projects. It makes CommonJS imports work like ES modules.
Migration and setup
Create the database schema with:
DATABASE_URL=postgres://user:password@localhost/mydb npx permly migrate
Add --schema auth if you want to use a different schema (the schema must exist first).
The migrate command is idempotent: running it multiple times is safe.
Common mistakes
Forgetting esModuleInterop — import pg won't work without it. Check your tsconfig.json.
Using connectionString without pool — Always use a pool for multiple connections. A single client will block other queries.
Wrong schema — If you pass schema: "auth", the schema must already exist. Postgres won't create it for you.
**Assuming :5432 — Postgres doesn't default to port 5432 on all platforms. Always specify the port.
Related pages
- Installation — How to set up permly
- Quick Start — Your first permissions check
- MySQL & MariaDB — Use MySQL instead
- MongoDB — Use MongoDB instead