
PostgREST serves a fully RESTful API from any existing PostgreSQL database. It provides a cleaner, more standards-compliant, faster API than you are likely to write from scratch.
Sponsors
|
|
|
|
|
|
postgrest is a free, open source databases project written in Haskell and released under MIT. It has 27,670 GitHub stars, 1,229 forks and 400 open issues, and was last pushed 4 hours ago. On this registry it ranks #19 of 81 tracked projects in Databases, with 5 head-to-head comparisons available. It gained 2 stars over the last 3 tracked days.
PostgREST is a Haskell-written server that serves a fully RESTful API from any existing PostgreSQL database, and it is aimed at developers, database administrators, and operators who want a standards-compliant HTTP API without writing and maintaining one from scratch.
PostgREST is a standalone HTTP server, written in Haskell with the Warp HTTP server, that exposes an existing PostgreSQL database as a RESTful API. It uses the Hasql library to keep a pool of database connections, speak the PostgreSQL binary protocol, and remain stateless so the server can scale horizontally. Authentication is handled via JSON Web Tokens, and authorization is delegated to role information defined inside the database. The server assumes the identity of the authenticated user for the duration of the connection, so it cannot do anything that user could not do. Versioning is performed through database schemas, and OpenAPI output provides self-documentation that tools such as Swagger-UI can render.
The concrete problem it solves is the custom API layer that teams otherwise write, maintain, and secure by hand. Rather than relying on an Object Relational Mapper and custom imperative coding, PostgREST requires declarative constraints directly in the database and delegates as much calculation as possible to PostgreSQL: serializing JSON responses in SQL, validating data, enforcing authorization, combining row counting and retrieval, and posting data in a single command with returning *. The README describes the result as a cleaner, more standards-compliant, faster API than one written from scratch. It lives in the PostgreSQL ecosystem and replaces the hand-written REST API and ORM-centric data access layer that would otherwise sit between a database and its clients.
returning *) to SQL.postgrest/postgrest.Install PostgREST on your platform according to the documentation at postgrest.org, or use the Docker image postgrest/postgrest; the Hackage package is postgrest, and postgrest --help prints invocation help.
The facts name no comparable tools or paid products that PostgREST replaces. It stands alone in this registry.
Choose PostgREST when PostgreSQL is the source of truth and when security, versioning, and validation can live in database roles, schemas, views, and declarative constraints. A self-hoster must operate PostgreSQL and provide JWT-based authentication; the README does not mention SMTP, object storage, or other external services. Skip it when the application needs imperative server-side logic, an Object Relational Mapper, or authorization and integrity rules outside the database, and note that the repository carries 400 open issues.
Big thanks to our sponsors! You can join them by supporting PostgREST on Patreon.
See the docs for how to install PostgREST on your platform. You can also use Docker.
Invoke for help:
postgrest --help
Latest documentation is at postgrest.org. You can contribute to the docs in PostgREST/postgrest/docs.
TLDR; subsecond response times for up to 2000 requests/sec on Heroku free tier. If you're used to servers written in interpreted languages, prepare to be pleasantly surprised by PostgREST performance.
Three factors contribute to the speed. First the server is written in Haskell using the Warp HTTP server (aka a compiled language with lightweight threads). Next it delegates as much calculation as possible to the database including
returning *)Finally it uses the database efficiently with the Hasql library by
PostgREST handles authentication (via JSON Web Tokens) and delegates authorization to the role information defined in the database. This ensures there is a single declarative source of truth for security. When dealing with the database the server assumes the identity of the currently authenticated user, and for the duration of the connection cannot do anything the user themselves couldn't. Other forms of authentication can be built on top of the JWT primitive. See the docs for more information.
A robust long-lived API needs the freedom to exist in multiple versions. PostgREST does versioning through database schemas. This allows you to expose tables and views without making the app brittle. Underlying tables can be superseded and hidden behind public facing views.
PostgREST uses the OpenAPI standard to generate up-to-date documentation for APIs. You can use a tool like Swagger-UI to render interactive documentation for demo requests against the live API server.
This project uses HTTP to communicate other metadata as well. For instance the number of rows returned by an endpoint is reported by - and limited with - range headers. More about that.
Rather than relying on an Object Relational Mapper and custom imperative coding, this system requires you to put declarative constraints directly into your database. Hence no application can corrupt your data (including your API server).
The PostgREST exposes HTTP interface with safeguards to prevent surprises, such as enforcing idempotent PUT requests.
See examples of PostgreSQL constraints and the API guide.
You can help PostgREST ongoing maintenance and development by making a regular donation through Patreon https://www.patreon.com/postgrest
Every donation will be spent on making PostgREST better for the whole community.
Contributions are always welcome and appreciated. Please see the Contributing guidelines.
The PostgREST organization is grateful to:
The cool logo came from Mikey Casalaina.
postgrest is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.
REST API for any Postgres database
postgrest is primarily written in Haskell. Its source is publicly available at https://github.com/PostgREST/postgrest, and it has 27,670 GitHub stars.