PostgREST
A standalone server that turns a PostgreSQL schema into a REST API governed by database roles, views, and functions.
Why consider PostgREST?
A standalone web server that automatically turns your PostgreSQL database into a RESTful API using the existing schema.
Learn PostgREST by building
Practical setup notes, real use cases, and copy-ready examples in one focused guide.
In this guide7 sections
PostgREST serves a fully RESTful API from any existing PostgreSQL database. It is a fast, declarative alternative to writing custom backend applications. In fact, PostgREST is a standalone web server that turns your PostgreSQL database directly into a RESTful API.
The Database as the API Foundation
When building applications, developers frequently write repetitive CRUD code. Using PostgREST is an alternative to manual CRUD programming. Because the server maps tables and views to URL paths, your application logic lives directly in the database. The structural constraints and permissions in the database determine the API endpoints and operations.
Instead of configuring a separate authentication layer in an application framework, you assign permissions natively using PostgreSQL roles. When a request comes in, PostgREST will switch into this role in the database to run queries. You can use your OS package manager to install PostgREST. Alternatively, you can run the application directly from pre-built binaries.
Focused Scope and Architecture
PostgREST has a focused scope. It works well with other tools like Nginx. This forces you to cleanly separate the data-centric CRUD operations from other concerns. Rather than building a monolithic application, you deploy a collection of sharp tools that each handle specific responsibilities efficiently. For instance, Nginx can be used as a reverse proxy to handle SSL termination, rate limiting, and request routing, while PostgREST is exclusively dedicated to transforming HTTP requests into SQL queries. This architectural pattern allows database administrators and backend engineers to build APIs from scratch with minimal custom programming outside of standard SQL statements.
Starting the Database Service
For new deployments or testing, you can begin by launching a PostgreSQL container. This ensures an isolated environment without conflicting with existing services. Run the following command to start the database service:
sudo docker run --name tutorial -p 5432:5432 \ -e POSTGRES_PASSWORD=notused \ -d postgresCreating the Schema and Tables
The underlying database configuration determines the API layout. Start by establishing a designated schema for the endpoints. Connect to the database and issue the schema creation command:
create schema api;With the schema established, you can define tables that represent the resources. For a basic checklist API, create a table with a few initial records:
create table api.todos ( id int primary key generated by default as identity, done boolean not null default false, task text not null, due timestamptz);insert into api.todos (task) values ('finish tutorial 0'), ('pat self on back');The server automatically identifies the api.todos table and creates a /todos endpoint for clients.
Configuring Access Roles
A production-ready database should restrict access. Instead of allowing external requests to use the highly privileged administration account, configure an anonymous role for unauthenticated web traffic:
create role web_anon nologin;grant usage on schema api to web_anon;grant select on api.todos to web_anon;This web_anon role specifically permits reading data from the defined schema without allowing login privileges. Next, create a dedicated role that the server uses to authenticate to the database:
create role authenticator noinherit login password 'mysecretpassword';grant web_anon to authenticator;Server Configuration and Execution
PostgREST relies on a configuration file to determine connection parameters. Create a configuration file containing the necessary database URI, the exposed schema, and the anonymous role:
db-uri = "postgres://authenticator:mysecretpassword@localhost:5432/postgres"db-schemas = "api"db-anon-role = "web_anon"Fetching the Data
Once the server is running, the RESTful API is immediately available for querying. Use a command-line HTTP client to test the exposed /todos endpoint:
curl http://localhost:3000/todosThe server responds with a JSON array reflecting the inserted table rows. For more comprehensive guides, consult the official documentation.
Related tools
More options with a similar category or technology profile.
diskus
Minimal, fast alternative to du -sh written in Rust using multi-threaded directory traversal.
peco
Simplistic interactive filtering tool for Unix pipelines, process lists, and file trees.
Dapr CLI
Command-line tool for managing Dapr distributed application runtime environments and sidecars.
Freeze
Generate beautiful image screenshots and SVGs of code snippets and terminal outputs.