GUIDE

How to give your Postgres tables a REST API your front end can call

Updated

Turn on the Data API for a Kaiva Cloud database. Each table gets an HTTPS address your front end reads and writes with fetch and a publishable key, with filters, order and paging in the query string, and nothing to run. Each table answers only what its rule opens: anyone with the key, signed-in users, or each user's own rows.

With Kaiva Cloud, nothing to run

Open the Data API page in the Kaiva Cloud console and turn it on for a database. It gets an address and a publishable key, sent as the apikey header. The key belongs in your app's code: it can do only what your table rules give anyone with the key.

Then give each table a rule for reading, adding, changing and deleting rows. Every table stays closed until you do. Signed-in users also send the token from Kaiva Cloud Authentication, and a rule can limit each of them to the rows that carry their id. New columns reach the Data API within a minute.

The steps below set up the same thing yourself, with PostgREST, for a Postgres database you run somewhere else.

Step by step

  1. Put the tables in their own schemaCreate a schema such as api for the tables your front end uses. PostgREST serves only the schemas you name, so everything else stays out of reach.
  2. Create the rolesOne role for visitors, which cannot log in and can only read the tables you grant it, and one login that PostgREST connects as, which can only switch into that role. The SQL is below.
  3. Run PostgRESTStart it with the login's connection string, the schema and the visitors' role. It answers on port 3000; put it behind HTTPS before your front end uses it.
  4. Call it from your front endRead a table with fetch and a query string, such as /todos?done=is.false&order=id.desc&limit=20. Each filter is a column, an operator and a value.
  5. Add signed-in usersGive PostgREST the secret your sign-in service signs tokens with. It checks the token on each request and switches to the role the token names, and a row level security policy compares each row's owner column with the token's sub claim.

PostgREST in Docker, then a first request:

Terminal
docker run --rm -p 3000:3000 \
  -e PGRST_DB_URI="postgres://authenticator:PASSWORD@HOST:5432/app" \
  -e PGRST_DB_SCHEMAS=api \
  -e PGRST_DB_ANON_ROLE=web_anon \
  postgrest/postgrest
curl "http://localhost:3000/todos?done=is.false&order=id.desc&limit=20"

The Data API from your front end

Copy the address and the key from the database's Data API page, and add a table's name to the address. A read returns the rows and whether there are more. Filters, order and pages work as listed under Filters, order and pages below. Changing or deleting rows needs at least one filter.

JavaScript
const TABLE = 'https://api.kaiv.ai/api/cloud/v1/data/YOUR_DATABASE/todos';
const KEY = 'YOUR_PUBLISHABLE_KEY';

const res = await fetch(`${TABLE}?select=id,task&done=eq.false&order=id.desc&limit=20`, {
  headers: { apikey: KEY },
});
const { rows, more } = await res.json();

Table rules

Each table has one rule, with a choice for each of reading, adding, changing and deleting rows. A table with no rule is closed to every request, and a closed table answers as if it did not exist. Changing or deleting rows also needs reading them, since a filter reads the column it compares.

ChoiceWho it lets in
NobodyNo one. Every table starts here.
Anyone with the keyAny page or script that has the publishable key
Signed-in usersRequests that carry a valid token from Kaiva Cloud Authentication
Each user's own rowsSigned-in users, limited to rows whose owner column holds their id

What stands behind the rules

Kaiva gives the Data API a database role of its own whose grants follow your rules, so the database itself refuses what a rule keeps closed. Allowed websites decide which sites' pages may use the key in a browser; they are not sign-in, since the key is public. Your app's server can still use the database's connection string, with full read and write access.

The roles, in SQL

Run this as the database's owner. Visitors can read todos and nothing else, and the authenticator login can do nothing until it switches to web_anon.

SQL
create schema api;
create table api.todos (
  id bigint generated always as identity primary key,
  task text not null,
  done boolean not null default false
);

create role web_anon nologin;
grant usage on schema api to web_anon;
grant select on api.todos to web_anon;

create role authenticator noinherit login password 'CHOOSE_A_PASSWORD';
grant web_anon to authenticator;

Filters, order and pages

Every query parameter other than select, order, limit and offset names a column, and its value is an operator and a value. When there are several filters, all of them must hold. select=id,task picks the columns, order=id.desc sorts, and limit and offset page through the rows.

OperatorMeansExample
eqequalsstatus=eq.open
neqdoes not equalstatus=neq.closed
gt, gte, lt, ltegreater or less thancreated_at=gte.2026-01-01
like, ilikematches a pattern, * for any text (ilike ignores case)name=ilike.*smith*
isis null, true or falsedeleted_at=is.null
inis one of a listid=in.(1,2,3)

Each user's own rows

Add a role for signed-in users, an owner column and one policy. PostgREST puts the verified token's claims in request.jwt.claims for each request, so the policy reads the user's id from there, and the column fills itself in when a row is added. Tokens must name web_user in their role claim, and PostgREST needs PGRST_JWT_SECRET set to your sign-in service's secret or its public keys.

With row level security on, visitors who are not signed in see no rows of this table unless another policy lets them.

SQL
create role web_user nologin;
grant usage on schema api to web_user;
grant select, insert, update, delete on api.todos to web_user;
grant web_user to authenticator;

alter table api.todos add column owner text
  default current_setting('request.jwt.claims', true)::json->>'sub';
alter table api.todos enable row level security;

create policy own_todos on api.todos to web_user
  using (owner = current_setting('request.jwt.claims', true)::json->>'sub');

Why not use the connection string in the browser

Anything in front-end code can be read by anyone who opens the page, and a connection string works with every right its login has. An HTTP API keeps the login on the server, and each request can do only what its role allows.

CORS settings decide which websites' pages may call the API from a browser. They do not stop a script or a server from calling it, so the roles and policies are what protect the data.

Other ways to do it

  • Your own API routesA route in your server for each thing the front end needs, such as an Express or Next.js route handler that runs one query. More code to write, with full control over every query.
  • Your host's REST APISupabase serves a PostgREST API for every project, and Neon offers a Data API for its databases. Both use row level security for each user's rows.
  • GraphQLHasura and PostGraphile serve the same tables as a GraphQL API, if your front end already uses GraphQL.

Questions

Can the front end use the database's connection string?
No. Anyone who opens the page can read it and use it with every right its login has. Keep it on a server, and give the front end an HTTP API.
Does PostgREST work with a database I already have?
Yes, at any host that lets you create roles. It needs a login, a schema to serve and roles to switch into.
How does a signed-in user's request differ?
It carries the user's token as Authorization: Bearer. The API checks the token, and the database's policies limit the user to their own rows.
How do I page through a large table?
Add order so the rows keep the same order, then limit and offset, such as order=id.desc&limit=50&offset=100.
Is the publishable key a secret?
No. It is meant to be in your page. It can do only what your table rules give anyone with the key, so keep a table on Nobody or Signed-in users when it should not be public.
Who can turn the Data API on?
Owners and admins of the workspace.