postgrest-docs
postgrest-docs copied to clipboard
Update postgrest-on-docker installation/tutorial documentation
As a Postgrest newbie, I recently managed to install and run Postgrest-on-docker-on-win10 but after much difficulty and great support from @laurenceisla because the guidance is misleading and incorrect in some cases - this is the original that I raised. The main reason is that it focuses on native installs, and there are gaps in the docker installation guidance - this applies to Tutorial 0, Tutorial 1, and Installation sections. I suggest a clear split in the guidance for the two processes as there are many differences. I found that the following process which I used, provides a clear, clean Postgres installation on docker/win10. i also found that installing pgadmin4 container to view db content is also a really useful feature - guidance is available here.
-------- STEP 1 ---------------------------
#load and run docker images with following docker-compose.yml content which starts postgres + postgrest + swagger servers and creates "postgres" as db owner with full rights.
> docker-compose up
version: '3'
services:
server:
image: postgrest/postgrest
ports:
- "3000:3000"
environment:
PGRST_DB_ANON_ROLE: postgres
PGRST_DB_URI: postgres://postgres:mysecretpassword@db:5432/postgres
PGRST_OPENAPI_SERVER_PROXY_URI: http://127.0.0.1:3000
PGRST_DB_SCHEMAS: api
depends_on:
- db
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: mysecretpassword
swagger:
image: swaggerapi/swagger-ui
ports:
- "8080:8080"
expose:
- "8080"
environment:
API_URL: http://localhost:3000/
# Uncomment this if you want to persist the data.
# volumes:
# - "C:\<your-volume-directory>:/var/lib/postgresql/data"
-------- STEP 2 -----------------------------
#start psql shell for with "postgres" as user - this will produce "postgres=#" cursor
> docker exec -it postgrest-db-1 psql -U postgres
-------- STEP 3 -----------------------------
execute following psql commands to create postgres db roles and tables for API
postgres=# create schema api;
create table api.todos (
id serial primary key,
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');
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 'mysecretpassword';
grant web_anon to authenticator;
-------- STEP 4 -----------------------------
execute curl command to invoke postgrest API which returns todos table content below
> curl http://localhost:3000/todos
StatusCode : 200
StatusDescription : OK
Content : [{"id":1,"done":false,"task":"finish tutorial 0","due":null},
{"id":2,"done":false,"task":"pat self on back","due":null}]
RawContent : HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Range: 0-1/*
Content-Location: /todos
Content-Type: application/json; charset=utf-8
Date: Wed, 12 Oct 2022 19:28:11 GMT
Server: postgrest/10.0....
Forms : {}
Headers : {[Transfer-Encoding, chunked], [Content-Range, 0-1/*], [Content-Location, /todos], [Content-Type, application/json; charset=utf-8]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 123