Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: update self-hosted setup

dragarciaa41a4f94420f
diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml
index 03a6f22a92d..46699f6c553 100644
--- a/docker/docker-compose.yml
+++ b/docker/docker-compose.yml
@@ -85,7 +85,7 @@ services:
restart: unless-stopped
environment:
PGRST_DB_URI: postgres://postgres:${POSTGRES_PASSWORD}@db:5432/postgres
- PGRST_DB_SCHEMAS: public,storage
+ PGRST_DB_SCHEMAS: public,storage, graphql_public
PGRST_DB_ANON_ROLE: anon
PGRST_JWT_SECRET: ${JWT_SECRET}
PGRST_DB_USE_LEGACY_GUCS: "false"
diff --git a/docker/volumes/api/kong.yml b/docker/volumes/api/kong.yml
index 452222651c4..a95baa779b1 100644
--- a/docker/volumes/api/kong.yml
+++ b/docker/volumes/api/kong.yml
@@ -96,19 +96,24 @@ services:
- anon
## Secure GraphQL routes
- - name: rest-v1
- _comment: "PostgREST: /rest/v1/* -> http://rest:3000/*"
- url: http://rest:3000/
+ - name: graphql-v1
+ _comment: "PostgREST: /rest/v1/* -> http://rest:3000/rpc/graphql"
+ url: http://rest:3000/rpc/graphql
routes:
- name: graphql-v1-all
strip_path: true
paths:
- - /graphql/v1/
+ - /graphql/v1
plugins:
- name: cors
- name: key-auth
config:
hide_credentials: true
+ - name: request-transformer
+ config:
+ add:
+ headers:
+ - Content-Profile:graphql_public
- name: acl
config:
hide_groups_header: true
diff --git a/docker/volumes/db/init/04-pg-graphql.sql b/docker/volumes/db/init/04-pg-graphql.sql
new file mode 100644
index 00000000000..a4929abdbae
--- /dev/null
+++ b/docker/volumes/db/init/04-pg-graphql.sql
@@ -0,0 +1,141 @@
+create schema if not exists graphql_public;
+
+-- GraphQL Placeholder Entrypoint
+create or replace function graphql_public.graphql(
+ "operationName" text default null,
+ query text default null,
+ variables jsonb default null,
+ extensions jsonb default null
+)
+ returns jsonb
+ language plpgsql
+as $$
+ DECLARE
+ server_version float;
+ BEGIN
+ server_version = (SELECT (SPLIT_PART((select version()), ' ', 2))::float);
+
+ IF server_version >= 14 THEN
+ RETURN jsonb_build_object(
+ 'data', null::jsonb,
+ 'errors', array['pg_graphql extension is not enabled.']
+ );
+ ELSE
+ RETURN jsonb_build_object(
+ 'data', null::jsonb,
+ 'errors', array['pg_graphql is only available on projects running Postgres 14 onwards.']
+ );
+ END IF;
+ END;
+$$;
+
+grant usage on schema graphql_public to postgres, anon, authenticated, service_role;
+alter default privileges in schema graphql_public grant all on tables to postgres, anon, authenticated, service_role;
+alter default privileges in schema graphql_public grant all on functions to postgres, anon, authenticated, service_role;
+alter default privileges in schema graphql_public grant all on sequences to postgres, anon, authenticated, service_role;
+
+alter default privileges for user supabase_admin in schema graphql_public grant all
+ on sequences to postgres, anon, authenticated, service_role;
+alter default privileges for user supabase_admin in schema graphql_public grant all
+ on tables to postgres, anon, authenticated, service_role;
+alter default privileges for user supabase_admin in schema graphql_public grant all
+ on functions to postgres, anon, authenticated, service_role;
+
+-- Trigger upon enabling pg_graphql
+CREATE OR REPLACE FUNCTION extensions.grant_pg_graphql_access()
+RETURNS event_trigger
+LANGUAGE plpgsql
+AS $func$
+ DECLARE
+ func_is_graphql_resolve bool;
+ BEGIN
+ func_is_graphql_resolve = (
+ SELECT n.proname = 'resolve'
+ FROM pg_event_trigger_ddl_commands() AS ev
+ LEFT JOIN pg_catalog.pg_proc AS n
+ ON ev.objid = n.oid
+ );
+
+ IF func_is_graphql_resolve
+ THEN
+ grant usage on schema graphql to postgres, anon, authenticated, service_role;
+ grant all on function graphql.resolve to postgres, anon, authenticated, service_role;
+
+ alter default privileges in schema graphql grant all on tables to postgres, anon, authenticated, service_role;
+ alter default privileges in schema graphql grant all on functions to postgres, anon, authenticated, service_role;
+ alter default privileges in schema graphql grant all on sequences to postgres, anon, authenticated, service_role;
+
+ create or replace function graphql_public.graphql(
+ "operationName" text default null,
+ query text default null,
+ variables jsonb default null,
+ extensions jsonb default null
+ )
+ returns jsonb
+ language sql
+ as $$
+ SELECT graphql.resolve(query, coalesce(variables, '{}'));
+ $$;
+
+ grant select on graphql.field, graphql.type, graphql.enum_value to postgres, anon, authenticated, service_role;
+ grant execute on function graphql.resolve to postgres, anon, authenticated, service_role;
+ END IF;
+
+ END;
+$func$;
+
+CREATE EVENT TRIGGER issue_pg_graphql_access ON ddl_command_end WHEN TAG in ('CREATE FUNCTION')
+EXECUTE PROCEDURE extensions.grant_pg_graphql_access();
+COMMENT ON FUNCTION extensions.grant_pg_graphql_access IS 'Grants access to pg_graphql';
+
+-- Trigger upon dropping the pg_graphql extension
+CREATE OR REPLACE FUNCTION extensions.set_graphql_placeholder()
+RETURNS event_trigger
+LANGUAGE plpgsql
+AS $func$
+ DECLARE
+ graphql_is_dropped bool;
+ BEGIN
+ graphql_is_dropped = (
+ SELECT ev.schema_name = 'graphql_public'
+ FROM pg_event_trigger_dropped_objects() AS ev
+ WHERE ev.schema_name = 'graphql_public'
+ );
+
+ IF graphql_is_dropped
+ THEN
+ create or replace function graphql_public.graphql(
+ "operationName" text default null,
+ query text default null,
+ variables jsonb default null,
+ extensions jsonb default null
+ )
+ returns jsonb
+ language plpgsql
+ as $$
+ DECLARE
+ server_version float;
+ BEGIN
+ server_version = (SELECT (SPLIT_PART((select version()), ' ', 2))::float);
+
+ IF server_version >= 14 THEN
+ RETURN jsonb_build_object(
+ 'data', null::jsonb,
+ 'errors', array['pg_graphql extension is not enabled.']
+ );
+ ELSE
+ RETURN jsonb_build_object(
+ 'data', null::jsonb,
+ 'errors', array['pg_graphql is only available on projects running Postgres 14 onwards.']
+ );
+ END IF;
+ END;
+ $$;
+ END IF;
+
+ END;
+$func$;
+
+CREATE EVENT TRIGGER issue_graphql_placeholder ON sql_drop WHEN TAG in ('DROP EXTENSION')
+EXECUTE PROCEDURE extensions.set_graphql_placeholder();
+COMMENT ON FUNCTION extensions.set_graphql_placeholder IS 'Reintroduces placeholder function for graphql_public.graphql';
\ No newline at end of file