TiloBox
Back to directory
PostgREST project preview

PostgREST

A standalone server that turns a PostgreSQL schema into a REST API governed by database roles, views, and functions.

LicenseMIT
GitHub stars27.6k
Last commit1 weeks ago
Tags7 topics
Rest ApiRow Level SecuritySelf HostedOpen SourceDeveloper ToolsHaskellPostgresql
Overview

Why consider PostgREST?

A standalone web server that automatically turns your PostgreSQL database into a RESTful API using the existing schema.

Guided learning

Learn PostgREST by building

Practical setup notes, real use cases, and copy-ready examples in one focused guide.

4 min read 7 sections
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:

bash
1sudo docker run --name tutorial -p 5432:5432 \
2 -e POSTGRES_PASSWORD=notused \
3 -d postgres

Creating 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:

postgres
1create 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:

postgres
1create table api.todos (
2 id int primary key generated by default as identity,
3 done boolean not null default false,
4 task text not null,
5 due timestamptz
6);
7
8insert into api.todos (task) values
9 ('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:

postgres
1create role web_anon nologin;
2
3grant usage on schema api to web_anon;
4grant 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:

postgres
1create role authenticator noinherit login password 'mysecretpassword';
2grant 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:

ini
1db-uri = "postgres://authenticator:mysecretpassword@localhost:5432/postgres"
2db-schemas = "api"
3db-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:

bash
1curl http://localhost:3000/todos

The 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.

PostgREST FAQs

PostgREST is listed as a Developer Tools tool on TiloBox. Review the overview, features, and official documentation on this page to decide whether it solves your specific workflow.

Start with the project's GitHub repository and official website for supported installation and deployment instructions. Test the setup with representative data or a small project before rolling it out more widely.

PostgREST is listed under the MIT license. Read the complete license text and the project's notices before using, modifying, or distributing the software.

Production readiness depends on your requirements. Review maintenance activity, security practices, documentation, backup and upgrade procedures, and compatibility with your stack; then validate it in a non-production environment.

PostgREST is listed as an alternative to Supabase. Compare the core workflow, deployment model, integrations, and licensing against your must-have requirements before switching.