PgRest
A PostgREST-compatible REST API powered by Elixir and Ecto, differential-tested against PostgREST v14.
PgRest serves the PostgREST HTTP API that @supabase/postgrest-js speaks, from
your own Phoenix or Plug app. Your Ecto schemas are the contract: each request
is parsed into an AST and compiled to one parameterized SQL statement per read,
with your scope/2 callbacks (tenancy, soft deletes) applied to the root and
every embed. It is a Plug that runs in your BEAM process on your Ecto repo; there
is no sidecar service. Compatibility is measured, not assumed: a differential
suite replays scenarios against a pinned PostgREST.
Requires Elixir 1.19+, PostgreSQL with an Ecto repository and a Plug-based application.
Installation
Add pgrest to your dependencies in mix.exs:
def deps do
[
{:pgrest, "~> 0.4.0"}
]
end
Then fetch dependencies:
mix deps.get
Quick Start
1. Define a resource
defmodule MyApp.API.Tasks do
use Ecto.Schema
use PgRest.Resource, custom_params: ["search"]
import Ecto.Query
schema "tasks" do
field :title, :string
field :status, :string
field :due_date, :date
field :deleted_at, :utc_datetime
field :internal_notes, :string, redact: true
belongs_to :project, MyApp.API.Projects
timestamps()
end
# Optional: always-applied query scope (tenant isolation, soft deletes)
@impl PgRest.Resource
def scope(query, _context), do: where(query, [t], is_nil(t.deleted_at))
# Optional: custom URL params (?search=foo), declared in `use` above
@impl PgRest.Resource
def handle_param("search", value, query, _context) do
where(query, [t], ilike(t.title, ^"%#{value}%"))
end
def handle_param(_, _, query, _), do: query
end
2. Start the registry
children = [
MyApp.Repo,
{PgRest.Registry, otp_app: :my_app}
]
3. Mount the plug
# Phoenix router
forward "/api", PgRest.Plug, repo: MyApp.Repo
# Plug.Router
forward "/api", to: PgRest.Plug, init_opts: [repo: MyApp.Repo]
Set extra_search_path: ["public", "extensions"] to resolve extension types such as PostGIS
geometry installed outside public (PostgREST's db-extra-search-path); the default is ["public"].
Accept: application/geo+json answers a GeoJSON FeatureCollection on databases with PostGIS, as
PostgREST does; see the PostGIS guide. Spatial filter operators are not supported.
4. Query with any PostgREST client
import { PostgrestClient } from '@supabase/postgrest-js'
const api = new PostgrestClient('http://localhost:4000/api')
const { data } = await api
.from('tasks')
.select('title,status,project(name)')
.eq('status', 'pending')
.order('due_date', { ascending: true })
.limit(10)
Or with plain HTTP:
GET /api/tasks?status=eq.pending&select=title,status,project(name)&order=due_date.asc&limit=10
Demo
demo/ is a Phoenix
LiveView query explorer. It runs example queries against seeded data with
@supabase/postgrest-js and shows the client code, generated SQL, response and
timing for each.
bin/pgrest start # Hivemind, serves localhost:4042
bin/pgrest stop
bin/pgrest console # remote shell into the demo node
Or without Hivemind: cd demo && mix setup && mix phx.server.
Documentation
- Configuration: plug options, registry, body parsing, statement timeout, repo logging
- Resources: resource options,
allow:, schema prefixes, column exposure, security notes - Functions and RPC: declarations, introspection, scoped results and response overrides
- Filtering Ecto queries: PostgREST filters on your own Ecto queries with
PgRest.Ecto.Filter - Authorization and RLS: policies, roles and security boundaries
- PostGIS and GeoJSON:
application/geo+json, GeoJSON in ordinary JSON,extra_search_path - Compatibility: supported operations, request pipeline, known differences from PostgREST
- Telemetry: events and metadata
- Testing: the docker compose test harness
- Upgrading from 0.1 to 0.2
- CHANGELOG
- HexDocs for the API reference
Development
docker compose up -d db
mix test
mix quality
License
MIT