API Reference

Scopes & Permissions

How HTTP PostgreSQL Gateway translates API keys into least-privilege PostgreSQL database roles.

Scopes & Permissions Reference

Unlike traditional database proxies that execute all queries under a single shared superuser connection, HTTP PostgreSQL Gateway enforces database-level authorization.

API keys are mapped to dynamically provisioned, dedicated PostgreSQL roles with the smallest possible set of database privileges. PostgreSQL itself enforces access control on every query.


CRUD Scope Letters

Scopes are specified using four lowercase letters:

Scope LetterSQL EquivalentDescription
cINSERTCreate new records.
rSELECTRead existing records. Also required for RETURNING clauses.
uUPDATEModify existing records.
dDELETERemove existing records.

Scope Definitions

When creating or updating an API key via POST /admin/keys, you can configure two levels of permissions:

1. Database-Wide Scopes (scope-database)

Applies permissions across all current user tables in the assigned database:

{ "database": ["sales"], "scope-database": ["r"] }

Effect: Read-only access to all current user tables in the sales database.

2. Table-Specific Scopes (tables, scope-table)

Restricts permissions to specific named tables:

{ "database": ["sales"], "tables": ["public.orders", "public.order_items"], "scope-table": ["c", "r"] }

Effect: Can insert and select from public.orders and public.order_items, but cannot query any other table in sales.

3. Mixed Scopes

Combine database-wide and table-specific permissions:

{ "database": ["sales"], "scope-database": ["r"], "tables": ["public.audit_logs"], "scope-table": ["c"] }

Effect: Read all tables in sales, and insert into public.audit_logs.


How Roles Are Provisioned

  1. Deterministic Role Naming: The gateway calculates a unique cryptographic hash from the normalized database name and granted scopes.
  2. Dedicated PostgreSQL Role: The gateway uses its administrative pool connection to execute:
    • CREATE ROLE <role_name> WITH LOGIN PASSWORD '<derived_password>'
    • GRANT CONNECT ON DATABASE <db> TO <role_name>
    • GRANT USAGE ON SCHEMA <schema> TO <role_name>
    • GRANT SELECT, INSERT... ON TABLE <table> TO <role_name>
  3. Dedicated Query Pool: When queries authenticate with this API key, the gateway routes them to a dedicated connection pool authenticated as that specific PostgreSQL role.

Why PostgreSQL Privileges Are Additive

In PostgreSQL, privileges are additive. A role inherits any privileges granted to PUBLIC, any parent group roles, and default schema permissions.

If PUBLIC has CONNECT or CREATE on a database or schema:

  • A key requested as "table-only" would inadvertently have access to public tables.
  • The gateway actively audits effective privileges during key creation. If inherited grants conflict with the requested narrow scope, the gateway rejects creation with 409 UNSAFE_DATABASE_SCOPE.

Hardening PostgreSQL for Scoped Keys

Run these statements as an administrator to ensure narrow scopes can be safely provisioned:

-- Revoke PUBLIC connection to the database REVOKE CONNECT ON DATABASE target_db FROM PUBLIC; \connect target_db -- Revoke default PUBLIC table creation in public schema REVOKE CREATE ON SCHEMA public FROM PUBLIC;

Re-binding Keys After Schema Changes

Database-wide grants apply to the tables present at the moment the key was saved.

If you add a new table to your database:

  1. The existing scoped role does not automatically receive privileges on the new table.
  2. To grant access to newly created tables or updated sequences, re-save the key using PATCH /admin/keys/:id.
  3. If an older key requires re-binding, queries will return 401 API_KEY_SCOPE_REBIND_REQUIRED until an administrator updates its scope.
Copyright © 2026