Questions tagged [postgrest]

PostgREST serves a fully RESTful API from any existing PostgreSQL database. It provides a cleaner, more standards-compliant, faster API. It differs from the other emerging API servers by taking an opinionated stance on the full implementation of API by being operationally sound and simple to deploy, by delivering built-in security with JSON Web Tokens plus database roles, and by fully embracing the relational model.

Introduction

PostgREST is a standalone web server that turns your PostgreSQL database directly into a RESTful API. The structural constraints and permissions in the database determine the API endpoints and operations.

Motivation

Using PostgREST is an alternative to manual CRUD programming. Custom API servers suffer problems. Writing business logic often duplicates, ignores or hobbles database structure. Object-relational mapping is a leaky abstraction leading to slow imperative code. The PostgREST philosophy establishes a single declarative source of truth: the data itself.

Declarative Programming

It's easier to ask Postgres to join data for you and let its query planner figure out the details than to loop through rows yourself. It's easier to assign permissions to db objects than to add guards in controllers. (This is especially true for cascading permissions in data dependencies.) It's easier to set constraints than to litter code with sanity checks.

Leakproof Abstraction

There is no ORM involved. Creating new views happens in SQL with known performance implications. A database administrator can now create an API from scratch with no custom programming.

Embracing the Relational Model

In 1970 E. F. Codd criticized the then-dominant hierarchical model of databases in his article A Relational Model of Data for Large Shared Data Banks. Reading the article reveals a striking similarity between hierarchical databases and nested http routes. With PostgREST we attempt to use flexible filtering and embedding rather than nested routes.

One Thing Well

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. Use a collection of sharp tools rather than building a big ball of mud.

136 questions
1
vote
1 answer

PostgreSQL does not find function

I have defined a function in my PostgreSQL 10.18 database like this: CREATE OR REPLACE FUNCTION public.log_event(event json) RETURNS void LANGUAGE 'plpgsql' AS $BODY$ BEGIN INSERT INTO "eventlog" VALUES(event::json->'event_type', …
Halmackenreuter
  • 681
  • 5
  • 9
1
vote
0 answers

Return nested data from recursive query

We have a list of recipes (recipe_id) and ingredients (ingredient_id). Every recipe can become an ingredient of another recipe and that child recipe can then hold more second level child recipes and so on. I need a recursive query that takes a…
user1634149
  • 71
  • 1
  • 2
1
vote
1 answer

PostgREST select with joined table where key in joined table

I want to join two tables using PostgREST, with a where condition that looks at the second table. My code so far: https://127.0.0.1/table_name_1?select=*,table_name_2(column_t2)&table_name_2(column_t2)=ilike.*some_value* This does not work, as it…
renaise21
  • 169
  • 2
  • 9
1
vote
1 answer

postgREST route using function does not update table

I am using Supabase which has a built in postgREST server. In this server I have created a function that should update a value given a checksum: CREATE OR REPLACE FUNCTION set_value(_id TEXT, _value INTEGER, _check INTEGER) RETURNS BOOLEAN AS…
Felipe
  • 10,606
  • 5
  • 40
  • 57
1
vote
0 answers

Django - Convert UUID back to default ID

Is it possible to convert back from UUID to Django's default ID? Could there be any impact in the existing data? If I delete this line in my Model, Django could automatically add a field to hold the primary key? Any thoughts on this or previous…
Jorge
  • 105
  • 9
1
vote
1 answer

406 JSON object requested, multiple (or no) rows when RLS is enabled on Supabase

I have looked at various solutions on StackOverflow, github issues in supabase, supabase/postgrest-js, postgRESTPostgREST/postgrest, and searched the Discord as well. But none of the solutions have been working so far. The code works as expected,…
1
vote
0 answers

ohmyfetch (Nuxt 3 $fetch) how to remove 'text/plain;charset=UTF-8' from content-type header?

I am trying to send a post request to PostgREST to invoke a stored procedure: const data = await $fetch('http://http-logger:3001/rpc/test_proc', { method: 'post', headers: { Prefer: 'params=single-object', Accept:…
ianj
  • 11
  • 4
1
vote
1 answer

How to limit max return rows of PostGRest?

I'm running a Postgrest api on a database with roughly 30 million rows. Sometimes I accidentally query the whole table without a ?limit parameter, and it severely lags the server. I also plan on making this public, and I do not want other users to…
BigP
  • 117
  • 7
1
vote
0 answers

With PostgREST, convert a column to and from an external encoding in the API

We are using PostgREST to automatically generate a REST API for a Postgres database. Our primary keys have an external representation that's different from how we store them internally. For simplicity's sake lets pretend the ids are stored as…
Alexander Ljungberg
  • 6,302
  • 4
  • 31
  • 37
1
vote
1 answer

Is it possible to copy an enum type from one schema to another

I'm using postgREST to generate a http accessible api (great stuff). There is a little bug that I'm trying to work around. For whatever reason, when calling a function, and only in the function parameters, I cannot use the types the api can…
Edmund's Echo
  • 766
  • 8
  • 15
1
vote
1 answer

Return result of three SQL queries in one SQL view

Is it possible to return results of three separate SQL queries in one view? I would like to create an "Initial data" view, with some stats from my DB, returning three counts() from different tables. Something like that: CREATE VIEW initial AS …
jkulak
  • 768
  • 1
  • 6
  • 18
1
vote
0 answers

Nested query in postgrest (supabase) denies existence of relationship between two tables

I have the following structure create table organisation ( id uuid primary key, organisation_name varchar not null ); create table organisation_member ( id uuid primary key, email varchar not null, name varchar not null, organisation…
Alb
  • 1,063
  • 9
  • 33
1
vote
1 answer

How to open PostgREST geojson web service in QGIS?

I have a PostgreSql stored procedure which returns a geojson feature collection created with json_build_object function. This web service opens fine in OpenLayers, however, when I try to open it in QGIS, with data source manager -> vector ->…
sigeal
  • 111
  • 5
1
vote
1 answer

PostgREST return not null values of JSON object

I have a postgrSQL table with three columns, one of which is a json field: id [num] || school [char] || info [json] ============================================================================ 1 || 1st || [{"fistname":…
renaise21
  • 169
  • 2
  • 9
1
vote
1 answer

PostgREST returns no error with wrong JSON values (Python request)

I'm trying to make a client for postgREST (latest version with PostgreSQL 13) When I tried to insert data, I felt on (what seems to me) a strange behavior: when I use json.dumps for an insert request, event if my value are wrong, I get a 201 code in…
OldPoorDev
  • 13
  • 1
  • 5